1 Set Up

1.1 R Code

#packages we need for this code file
library(ggplot2)
library(mgcv)
library(lubridate)
library(zoo)
library(tidyverse)
library(dplyr)
library(DHARMa)
library(mgcViz)
library(extrafont)
library(arm)
loadfonts()
library(stargazer)
library(ellipse)
library(dotwhisker)
library(countreg)
#define functions we will need for analysis
#expit function
expit<-function(x){
  return(exp(x)/(1 + exp(x)))
}

#logit function
logit<-function(x){
  return(log(x/(1 - x)))
}

1.2 Data

#read in data
main_analysis_data<-read.csv("./Data/full_data_set_11_29_21_unintentional.csv")

################################## set up data set ################################
#add the intervention dates and time period data
main_analysis_data$Intervention_First_Date<-as.Date(main_analysis_data$Intervention_First_Date)
main_analysis_data$Time_Period_Start<-as.Date(main_analysis_data$Time_Period_Start)
names(main_analysis_data)[which(colnames(main_analysis_data) == "sum_deaths")] <- "imputed_deaths"

################################## set up the Regions ##############################
#set up the regions according to Census: https://www.census.gov/geographies/reference-maps/2010/geo/2010-census-regions-and-divisions-of-the-united-states.html
NE.name <- c("Connecticut","Maine","Massachusetts","New Hampshire",
             "Rhode Island","Vermont","New Jersey","New York",
             "Pennsylvania")

MW.name <- c("Indiana","Illinois","Michigan","Ohio","Wisconsin",
             "Iowa","Kansas","Minnesota","Missouri","Nebraska",
             "North Dakota","South Dakota")

S.name <- c("Delaware","District of Columbia","Florida","Georgia",
            "Maryland","North Carolina","South Carolina","Virginia",
            "West Virginia","Alabama","Kentucky","Mississippi",
            "Tennessee","Arkansas","Louisiana","Oklahoma","Texas")

W.name <- c("Arizona","Colorado","Idaho","New Mexico","Montana",
            "Utah","Nevada","Wyoming","Alaska","California",
            "Hawaii","Oregon","Washington")

region.list <- list(
  Northeast=NE.name,
  Midwest=MW.name,
  South=S.name,
  West=W.name)

#initialize vector with "West" and then impute the other regions for the states
main_analysis_data$Region<-rep("West", nrow(main_analysis_data))
for(state in unique(main_analysis_data$State)){
  if(state %in% region.list$Northeast){
    main_analysis_data$Region[main_analysis_data$State == state]<-"Northeast"
  }else if(state %in% region.list$Midwest){
    main_analysis_data$Region[main_analysis_data$State == state]<-"Midwest"
  }else if(state %in% region.list$South){
    main_analysis_data$Region[main_analysis_data$State == state]<-"South"
  }
}

2 Sandwich Estimator Code

#here, we estimate the variance-covariance matrix through the sandwich estimator
#we create a function so that we don't have to keep writing the code:
#cov_data is such that rows are state-time combinations and columns are the different policy measures
#coef_values need to be in order of the columns of cov_data
#z_value is the z-value that corresponds to the CI. We default to 95% CI so we default to 1.96
#we take k as the number of parametesr for a bias correction

compute_sd_and_CI_linear_link <- function(cov_data, observed_y, coef_values, z_value = 1.96, k,
                              print_full_cov = FALSE){
  middle_term <- matrix(0, nrow = ncol(cov_data), ncol = ncol(cov_data))
  for(i in 1:nrow(cov_data)){
    #sum_{s,t} (z_{s,t}z_{s,t}^T)*(y_{s,t}-z_{s,t}^T theta)^2
    middle_term <- middle_term + tcrossprod(as.matrix(cov_data[i,]))*
      as.numeric((observed_y[i] - t(as.matrix(cov_data[i,]))%*%coef_values)^2)
  }
  #(Z^T Z)^{-1}*middle_term*(Z^T Z)^{-1}
  crossproduct_cov_data_inverse <- solve(crossprod(cov_data))
  var_cov <- crossproduct_cov_data_inverse%*%(middle_term)%*%crossproduct_cov_data_inverse*(nrow(cov_data)/(nrow(cov_data) - k))
  #we obtain the standard deviations by taking the square root of the diagonal of the variance-covariance matrix.
  sd_of_coefficients <- sqrt(diag(var_cov))
  
  #find the CI for the coefficients
  lb_coef <- coef_values - z_value*(sd_of_coefficients)
  ub_coef <- coef_values + z_value*(sd_of_coefficients)
  
  return_data_set <- data.frame(lb_coef, coef_values, ub_coef, sd_coef = sd_of_coefficients)
  
  if(print_full_cov){
    return(list(return_data_set = return_data_set, var_cov = var_cov))
  }else{
    return(return_data_set)
  }
}

compute_sd_and_CI_logistic <- function(cov_data, observed_y, coef_values, z_value = 1.96,
                              k,
                              print_full_cov = FALSE){
  C_term <- middle_term <- matrix(0, nrow = ncol(cov_data), ncol = ncol(cov_data))
  for(i in 1:nrow(cov_data)){
    #p_{s,t} = expit(z_{s,t}^T Theta)
    pst <- expit(t(as.matrix(cov_data[i,]))%*%coef_values)
    
    #sum_{s,t} z_{s,t} z_{s,t}^T (y_{s,t} - p_{s,t})
    middle_term <- middle_term + tcrossprod(as.matrix(cov_data[i,]))*
      as.numeric((observed_y[i] - pst)^2)
    
    #sum_{s,t} z_{s,t} z_{s,t}^T p_{s,t}*(1-p_{s,})
    C_term <- C_term + tcrossprod(as.matrix(cov_data[i,]))*as.numeric(pst*(1 - pst))
  }

  inverse_C_term <- solve(C_term)
  
  var_cov <- inverse_C_term%*%as.matrix(middle_term)%*%inverse_C_term*(nrow(cov_data)/(nrow(cov_data) - k))

  #we obtain the standard deviations by taking the square root of the diagonal of the variance-covariance matrix.
  sd_of_coefficients <- sqrt(diag(var_cov))

  #find the CI for the coefficients
  lb_coef <- coef_values - z_value*(sd_of_coefficients)
  ub_coef <- coef_values + z_value*(sd_of_coefficients)

  return_data_set <- data.frame(lb_coef, coef_values, ub_coef,
             exp_lb = exp(lb_coef), exp_coef = exp(coef_values),
             exp_ub = exp(ub_coef), sd_coef = sd_of_coefficients)


  if(print_full_cov){
    return(list(return_data_set = return_data_set, var_cov = var_cov))
  }else{
    return(return_data_set)
  }
}

3 Attributable Deaths Computation

attr_death_compute <- function(data, coef_data,  tx_name = NULL, 
                               var_cov_mat_beta = NULL, prob_of_od_var = "prop_dead"){
  attr_table <- data.frame(matrix(NA, nrow = unique(data$Time_Period_ID), ncol = 4)) 
  #filter data so that it's only states where there was treatment
  treated_data <- data %>%
    filter(Intervention_Redefined > 0)
  
  for(time in unique(treated_data$Time_Period_ID)){
    #filter treated_data to time period t
    time_data <- treated_data %>%
      filter(Time_Period_ID == time)
    
    #obtain the population
    pop <- time_data$population
    
    #obtain the estimated probability had intervention not occurred
    #here, we compute x^T*beta where x is a vector of the covariates and beta is the corresponding coefficients
    est_prob_no_int <- time_data[,prob_of_od_var]*exp(- as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "estimate"]))
    
    #estimated number of OD had intervention not occurred
    n_od_no_int <- pop*est_prob_no_int
    
    #obtain LB and UB
    if(length(tx_name) == 1){
      #if there is only one variable corresponding to treatment, we don't need to account for covariances
      est_prob_no_int_lb <- time_data[,prob_of_od_var]*exp(- as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "conf.low"]))
      n_attr_od_lb <- sum(pop*est_prob_no_int_lb) - sum(time_data$imputed_deaths)
      
      est_prob_no_int_ub <- time_data[,prob_of_od_var]*exp(-as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "conf.high"]))
      n_attr_od_ub <- sum(pop*est_prob_no_int_ub) - sum(time_data$imputed_deaths)
    }else{
      #otherwise, if there are more than 1 treatment parameters, we need to account for covariances
      #here we assume its two treatment parameters 
      jacobian_first_entry <- sum(time_data$population*time_data[,prob_of_od_var]*
                                    exp(-as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name,"estimate"]))*
                                    time_data[,tx_name[1]])
      jacobian_second_entry <- sum(time_data$population*time_data[,prob_of_od_var]*
                                     exp(-as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name,"estimate"]))*
                                     time_data[,tx_name[2]])
      
      jacobian_mat <- matrix(c(jacobian_first_entry, jacobian_second_entry), nrow = 1)
      
      computed_sd <- sqrt(tcrossprod(jacobian_mat%*%var_cov_mat_beta, jacobian_mat)) 
      n_attr_od_lb <- sum(n_od_no_int - time_data$imputed_deaths) - 1.96*computed_sd
      n_attr_od_ub <- (sum(n_od_no_int - time_data$imputed_deaths)) + 1.96*computed_sd
      
    }
    
    attr_table[time,] <- c(time, sum(n_od_no_int - time_data$imputed_deaths), 
                           (n_attr_od_lb) , 
                           (n_attr_od_ub) )
    
    
  }
  colnames(attr_table) <- c("Time_Period", "attr_deaths", "attr_deaths_lb", "attr_deaths_ub")
  attr_table
}

4 Event Study Data Creation

main_analysis_data$prop_dead <- main_analysis_data$imputed_deaths/main_analysis_data$population
#create the dataset for the event study to check for pre-trend analysis
time_data_int <- main_analysis_data %>%
  #group by the state
  group_by(State) %>%
  #find the time interval ID for the intervention time
  summarise(intervention_time_id = ifelse(floor_date(Intervention_First_Date, "6 months") == Time_Period_Start, Time_Period_ID, NA)) %>%
  #filter out the other time periods that aren't the intervention date
  filter(!is.na(intervention_time_id))

#merge the time_data_int with the main dataset
merged_main_time_data_int <- merge(main_analysis_data, time_data_int, by = "State", all.x = TRUE)

#create the columns that associate with the periods before the intervention
#the max number of periods before the intervention is determined by the maximum time period of the intervention
neg_periods_df <- sapply(1:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 1),
                         #the indicator for x periods before intervention is equal to 1 if the time ID of intervention minus time ID is equal to x
                         function(x){ifelse(merged_main_time_data_int$intervention_time_id - 
                                              merged_main_time_data_int$Time_Period_ID == x, 1, 0)})

#create the column names
colnames(neg_periods_df) <- sapply(1:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 1), 
                                   function(x){paste("neg_", x, "_pd", sep = "")})
#add in the state and time ID columns
neg_periods_df <- cbind(neg_periods_df, "State" = merged_main_time_data_int$State, 
                        "Time_Period_ID" = merged_main_time_data_int$Time_Period_ID)
#for Hawaii, impute a 0 because it is NA right now
neg_periods_df[neg_periods_df[,"State"] == "Hawaii", 1:34] <- 0

#create the columns that associate with the periods after the intervention
#the max number of periods after the intervention is determined by the maximum Time ID minus the minus time period of the intervention
#the period 0 is associated with intervention time
pos_periods_df <- sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                         function(x){ifelse(merged_main_time_data_int$Time_Period_ID - 
                                              merged_main_time_data_int$intervention_time_id == x, 1, 0)})
#create the column names
colnames(pos_periods_df) <- sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                                        min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")})
#add in the state and time ID columns
pos_periods_df <- cbind(pos_periods_df, "State" = merged_main_time_data_int$State, 
                        "Time_Period_ID" = merged_main_time_data_int$Time_Period_ID)
#for Hawaii, impute a 0 because it is NA right now
pos_periods_df[pos_periods_df[,"State"] == "Hawaii", 1:40] <- 0

#merge the columns of indicators for before and after the intervention with the main analysis data to create the dataset for event study
sensitivity_anlys_event_study_data <- merge(main_analysis_data, 
                                            neg_periods_df, by = c("State", "Time_Period_ID"))

sensitivity_anlys_event_study_data <- merge(sensitivity_anlys_event_study_data, 
                                            pos_periods_df, by = c("State", "Time_Period_ID"))
#change the indicator values to numeric type 
neg_1_index <- which(colnames(sensitivity_anlys_event_study_data) == "neg_1_pd")
pos_39_index <- which(colnames(sensitivity_anlys_event_study_data) == "pos_39_pd")

sensitivity_anlys_event_study_data[, neg_1_index:pos_39_index] <- apply(sensitivity_anlys_event_study_data[, neg_1_index:pos_39_index], 
                                                                        2, as.numeric)

5 OLS Model Main Analysis With Smoothed Time Effects With Log Proportion

#compute the proportion of people who died from drug overdose
main_analysis_data$prop_dead <- main_analysis_data$imputed_deaths/main_analysis_data$population
#add the intervention variable as a measure of number of states with DIH prosecution
main_analysis_data <- main_analysis_data %>%
  group_by(Time_Period_Start) %>%
  mutate(num_states_w_intervention = sum(Intervention_Redefined))

#fit an OLS with smoothed time effects
main_analysis_model_log_smoothed_time<-gam(log(prop_dead)~ State +
                                             s(Time_Period_ID, bs = "cr", by = as.factor(Region)) +
                                             Naloxone_Pharmacy_Yes_Redefined +
                                             Naloxone_Pharmacy_No_Redefined +
                                             Medical_Marijuana_Redefined +
                                             Recreational_Marijuana_Redefined +
                                             GSL_Redefined +
                                             PDMP_Redefined +
                                             Medicaid_Expansion_Redefined +
                                             Intervention_Redefined +
                                             num_states_w_intervention,
                                           data = main_analysis_data)

summary(main_analysis_model_log_smoothed_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + Naloxone_Pharmacy_No_Redefined + 
##     Medical_Marijuana_Redefined + Recreational_Marijuana_Redefined + 
##     GSL_Redefined + PDMP_Redefined + Medicaid_Expansion_Redefined + 
##     Intervention_Redefined + num_states_w_intervention
## 
## Parametric coefficients:
##                                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      -9.5901401  0.2059517 -46.565  < 2e-16 ***
## StateAlaska                       0.1642934  0.0743934   2.208 0.027331 *  
## StateArizona                      0.2883356  0.0677554   4.256 2.19e-05 ***
## StateArkansas                    -0.4868587  0.0667523  -7.294 4.40e-13 ***
## StateCalifornia                  -0.1561194  0.0743859  -2.099 0.035967 *  
## StateColorado                     0.0361092  0.0741355   0.487 0.626265    
## StateConnecticut                  0.2301798  0.0709034   3.246 0.001189 ** 
## StateDelaware                     0.2169779  0.0680081   3.190 0.001443 ** 
## StateFlorida                      0.3085987  0.0668104   4.619 4.11e-06 ***
## StateGeorgia                      0.0002845  0.0667934   0.004 0.996602    
## StateHawaii                      -0.3783288  0.0729745  -5.184 2.40e-07 ***
## StateIdaho                       -0.1242470  0.0668007  -1.860 0.063043 .  
## StateIllinois                     0.1612842  0.0678109   2.378 0.017483 *  
## StateIndiana                      0.0819325  0.0663673   1.235 0.217156    
## StateIowa                        -0.6769694  0.0665778 -10.168  < 2e-16 ***
## StateKansas                      -0.2307812  0.0660706  -3.493 0.000489 ***
## StateKentucky                     0.7028805  0.0667944  10.523  < 2e-16 ***
## StateLouisiana                    0.3407484  0.0659833   5.164 2.67e-07 ***
## StateMaine                        0.0880973  0.0738853   1.192 0.233271    
## StateMaryland                    -1.5131006  0.0677976 -22.318  < 2e-16 ***
## StateMassachusetts               -0.0833781  0.0673507  -1.238 0.215879    
## StateMichigan                     0.0099350  0.0683957   0.145 0.884522    
## StateMinnesota                   -0.6430300  0.0699022  -9.199  < 2e-16 ***
## StateMississippi                 -0.0490213  0.0660266  -0.742 0.457907    
## StateMissouri                     0.1675455  0.0682763   2.454 0.014219 *  
## StateMontana                     -0.4401972  0.0702845  -6.263 4.65e-10 ***
## StateNebraska                    -0.8742551  0.0670928 -13.031  < 2e-16 ***
## StateNevada                       0.4597148  0.0717591   6.406 1.87e-10 ***
## StateNew Hampshire                0.1476303  0.0671583   2.198 0.028051 *  
## StateNew Jersey                   0.0807155  0.0679571   1.188 0.235082    
## StateNew Mexico                   0.6453680  0.0729173   8.851  < 2e-16 ***
## StateNew York                    -0.1394535  0.0683701  -2.040 0.041518 *  
## StateNorth Carolina               0.2339425  0.0658631   3.552 0.000392 ***
## StateNorth Dakota                -1.1371560  0.0664632 -17.110  < 2e-16 ***
## StateOhio                         0.4525904  0.0669815   6.757 1.86e-11 ***
## StateOklahoma                     0.4639698  0.0664674   6.980 4.04e-12 ***
## StateOregon                      -0.2715116  0.0738537  -3.676 0.000243 ***
## StatePennsylvania                 0.5608113  0.0668628   8.387  < 2e-16 ***
## StateRhode Island                -0.2786824  0.0693850  -4.016 6.14e-05 ***
## StateSouth Carolina               0.2077684  0.0664045   3.129 0.001781 ** 
## StateSouth Dakota                -1.0271060  0.0667936 -15.377  < 2e-16 ***
## StateTennessee                    0.4686941  0.0656809   7.136 1.36e-12 ***
## StateTexas                       -0.0198795  0.0667433  -0.298 0.765850    
## StateUtah                        -0.0957968  0.0660724  -1.450 0.147257    
## StateVermont                     -0.1694961  0.0696873  -2.432 0.015097 *  
## StateVirginia                    -0.0323098  0.0659867  -0.490 0.624444    
## StateWashington                   0.0451432  0.0752379   0.600 0.548573    
## StateWest Virginia                0.7907588  0.0668153  11.835  < 2e-16 ***
## StateWisconsin                    0.0062720  0.0661277   0.095 0.924446    
## StateWyoming                     -0.0218261  0.0660516  -0.330 0.741103    
## Naloxone_Pharmacy_Yes_Redefined  -0.0802046  0.0424748  -1.888 0.059138 .  
## Naloxone_Pharmacy_No_Redefined   -0.0014359  0.0385076  -0.037 0.970259    
## Medical_Marijuana_Redefined       0.1928104  0.0306582   6.289 3.95e-10 ***
## Recreational_Marijuana_Redefined -0.1113138  0.0487806  -2.282 0.022603 *  
## GSL_Redefined                     0.0536153  0.0315983   1.697 0.089900 .  
## PDMP_Redefined                   -0.1526621  0.0246836  -6.185 7.58e-10 ***
## Medicaid_Expansion_Redefined      0.0909280  0.0301320   3.018 0.002581 ** 
## Intervention_Redefined           -0.0253718  0.0244012  -1.040 0.298574    
## num_states_w_intervention        -0.0042330  0.0069748  -0.607 0.543993    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.678  5.750 12.35  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.454  8.914 17.38  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.360  7.506 12.53  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      3.027  3.846 10.72  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.84   Deviance explained = 84.6%
## GCV = 0.089657  Scale est. = 0.086002  n = 2000
gam.check(main_analysis_model_log_smoothed_time, page = 1)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 5 iterations.
## The RMS GCV score gradient at convergence was 1.113005e-06 .
## The Hessian was positive definite.
## Model rank =  95 / 95 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                                                k'  edf k-index p-value
## s(Time_Period_ID):as.factor(Region)Midwest   9.00 4.68    1.05    0.98
## s(Time_Period_ID):as.factor(Region)Northeast 9.00 8.45    1.05    0.98
## s(Time_Period_ID):as.factor(Region)South     9.00 6.36    1.05    0.99
## s(Time_Period_ID):as.factor(Region)West      9.00 3.03    1.05    0.99
#examine fitted values
summary(fitted(main_analysis_model_log_smoothed_time))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -12.316 -10.207  -9.739  -9.796  -9.343  -8.122
hist(fitted(main_analysis_model_log_smoothed_time))

#smoothed effects
plot(main_analysis_model_log_smoothed_time, pages = 1)

5.1 Sandwich Estimator: Coefficients and 95% CI

#compute the full dataset including basis functions
full_df_w_basis_functions_log_smoothed_time <- as.matrix(data.frame(predict(main_analysis_model_log_smoothed_time, type = "lpmatrix")))

#estimate the 95% CI and SD
coefficient_values_log_smoothed_time <- coef(main_analysis_model_log_smoothed_time)
#type = "response" to get the estimated probabilities
main_analysis_sd_and_ci_log_smoothed_time <- compute_sd_and_CI_linear_link(full_df_w_basis_functions_log_smoothed_time,
                                                               log(main_analysis_data$prop_dead),
                                                               coefficient_values_log_smoothed_time,
                                                               k = ncol(full_df_w_basis_functions_log_smoothed_time))
# format(round(main_analysis_sd_and_ci_log_smoothed_time, 3), nsmall = 3)

colnames(main_analysis_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high", "sd")
main_analysis_sd_and_ci_log_smoothed_time$term <- rownames(main_analysis_sd_and_ci_log_smoothed_time)


main_analysis_sd_and_ci_log_smoothed_time$ci_95 <- 
  paste("95% CI = (", format(round(main_analysis_sd_and_ci_log_smoothed_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(main_analysis_sd_and_ci_log_smoothed_time$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(main_analysis_sd_and_ci_log_smoothed_time[51:59,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Exposure Intervention") + 
  scale_color_grey()    + 
  geom_text(main_analysis_sd_and_ci_log_smoothed_time[51:59,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 9:1), size = 3) + 
  geom_text(main_analysis_sd_and_ci_log_smoothed_time[51:59,], 
            mapping = aes(label = ci_95, x = 0.9, y = 9:1), size = 3) +
  xlim(-.3, 1.1)

main_analysis_coef_of_interest_RR <- main_analysis_sd_and_ci_log_smoothed_time[51:59,]
main_analysis_coef_of_interest_RR$est_RR <- round(exp(main_analysis_coef_of_interest_RR$estimate), 3)
main_analysis_coef_of_interest_RR$est_RR_lb <- round(exp(main_analysis_coef_of_interest_RR$conf.low), 3)
main_analysis_coef_of_interest_RR$est_RR_ub <- round(exp(main_analysis_coef_of_interest_RR$conf.high), 3)

main_analysis_coef_of_interest_RR[,c("est_RR", "est_RR_lb", "est_RR_ub")]
##                                  est_RR est_RR_lb est_RR_ub
## Naloxone_Pharmacy_Yes_Redefined   0.923     0.863     0.987
## Naloxone_Pharmacy_No_Redefined    0.999     0.936     1.065
## Medical_Marijuana_Redefined       1.213     1.133     1.298
## Recreational_Marijuana_Redefined  0.895     0.832     0.962
## GSL_Redefined                     1.055     0.999     1.115
## PDMP_Redefined                    0.858     0.813     0.906
## Medicaid_Expansion_Redefined      1.095     1.040     1.153
## Intervention_Redefined            0.975     0.931     1.021
## num_states_w_intervention         0.996     0.979     1.013

5.2 Attributable Deaths

date_data <- main_analysis_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_smoothed_time <- attr_death_compute(main_analysis_data, main_analysis_sd_and_ci_log_smoothed_time, 
                                                        tx_name = "Intervention_Redefined")
attr_deaths_est_log_smoothed_time <- merge(attr_deaths_est_log_smoothed_time, date_data, by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_smoothed_time, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

5.3 Event Study: All Periods

5.3.1 Model Fitting

#create a formula for the gam model which includes the state effects, smoothed time effects, policy measures, 
#the periods before the intervention (excluding 1 period and 34 periods before intervention)
#the intervention period, and the periods after the intervention

formula_event_study_log_smoothed_time <- formula(paste("log(prop_dead) ~ State +
                                           s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                           paste(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)-2), 
                                                        function(x)paste("neg_", x, "_pd", sep = "")), collapse = "+"),
                                           "+",
                                           paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                                                             min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                                                        function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_event_study_model_log_smoothed_time<-gam(formula_event_study_log_smoothed_time,
                                                           data = sensitivity_anlys_event_study_data)

# summary(sensitivity_anlys_event_study_model_log_smoothed_time)

5.3.2 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_event_study_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_event_study_log_smoothed_time <- coef(sensitivity_anlys_event_study_model_log_smoothed_time)
#type = "response" to get the estimated probabilities
sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time <-
  compute_sd_and_CI_linear_link(as.matrix(full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time), 
                    log(sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_event_study_log_smoothed_time,
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time))
(sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time)
##                                                     lb_coef  coef_values
## (Intercept)                                    -9.730462237 -9.607755377
## StateAlaska                                    -0.049423874  0.101715259
## StateArizona                                    0.144496017  0.257887425
## StateArkansas                                  -0.626540747 -0.516038962
## StateCalifornia                                -0.197605999 -0.062450464
## StateColorado                                  -0.121794519  0.002447472
## StateConnecticut                                0.087500824  0.222377470
## StateDelaware                                   0.018646885  0.155066167
## StateFlorida                                    0.292773480  0.412924559
## StateGeorgia                                    0.012368305  0.127609736
## StateHawaii                                    -0.610276068 -0.467150486
## StateIdaho                                     -0.276925613 -0.159066931
## StateIllinois                                   0.103069959  0.233581709
## StateIndiana                                   -0.024961041  0.076071921
## StateIowa                                      -0.734431274 -0.624312899
## StateKansas                                    -0.322260429 -0.224552743
## StateKentucky                                   0.591020827  0.686996193
## StateLouisiana                                  0.292097065  0.394888461
## StateMaine                                     -0.065746614  0.074857829
## StateMaryland                                  -1.675419104 -1.451851831
## StateMassachusetts                             -0.302002541 -0.083062578
## StateMichigan                                  -0.046464767  0.050576690
## StateMinnesota                                 -0.754905369 -0.645251331
## StateMississippi                               -0.210742379 -0.087268616
## StateMissouri                                   0.085057722  0.185702542
## StateMontana                                   -0.510770699 -0.394331541
## StateNebraska                                  -1.018596137 -0.912234589
## StateNevada                                     0.384505286  0.495672978
## StateNew Hampshire                              0.010674384  0.124591896
## StateNew Jersey                                -0.010708998  0.145727757
## StateNew Mexico                                 0.486968538  0.622210509
## StateNew York                                  -0.252658248 -0.132279799
## StateNorth Carolina                             0.186121130  0.274412387
## StateNorth Dakota                              -1.343680088 -1.172707883
## StateOhio                                       0.453871910  0.589151698
## StateOklahoma                                   0.336286341  0.453117480
## StateOregon                                    -0.416865124 -0.299059866
## StatePennsylvania                               0.559441441  0.677459526
## StateRhode Island                              -0.589873781 -0.312527565
## StateSouth Carolina                             0.045203737  0.161134027
## StateSouth Dakota                              -1.218317372 -1.084870928
## StateTennessee                                  0.382305713  0.470729691
## StateTexas                                     -0.033873294  0.087487525
## StateUtah                                      -0.241196453 -0.054041819
## StateVermont                                   -0.335254127 -0.191087648
## StateVirginia                                  -0.069301461  0.031405848
## StateWashington                                -0.096970886  0.024086450
## StateWest Virginia                              0.616272337  0.763862290
## StateWisconsin                                 -0.041315108  0.055350995
## StateWyoming                                   -0.158016158 -0.026862195
## Naloxone_Pharmacy_Yes_Redefined                -0.126819690 -0.061319716
## Naloxone_Pharmacy_No_Redefined                 -0.061827052  0.003373398
## Medical_Marijuana_Redefined                     0.129050881  0.199070518
## Recreational_Marijuana_Redefined               -0.175467189 -0.098626529
## GSL_Redefined                                   0.007698979  0.063283495
## PDMP_Redefined                                 -0.234541848 -0.178713621
## Medicaid_Expansion_Redefined                    0.037370623  0.089195898
## neg_2_pd                                       -0.092349592  0.020524738
## neg_3_pd                                       -0.094755826  0.016757110
## neg_4_pd                                       -0.131739932 -0.010783798
## neg_5_pd                                       -0.130295904 -0.013004948
## neg_6_pd                                       -0.117049085 -0.005778559
## neg_7_pd                                       -0.160704646 -0.044021509
## neg_8_pd                                       -0.245224820 -0.100544147
## neg_9_pd                                       -0.182129446 -0.025158944
## neg_10_pd                                      -0.118922597  0.024031356
## neg_11_pd                                      -0.125372091  0.025412131
## neg_12_pd                                      -0.050114192  0.115435529
## neg_13_pd                                      -0.152868158  0.015177292
## neg_14_pd                                      -0.174761759 -0.015729613
## neg_15_pd                                      -0.248669745 -0.059495317
## neg_16_pd                                      -0.217482729 -0.043767301
## neg_17_pd                                      -0.213246323 -0.060151411
## neg_18_pd                                      -0.196116002 -0.051908104
## neg_19_pd                                      -0.386274566 -0.154364075
## neg_20_pd                                      -0.449689904 -0.193711115
## neg_21_pd                                      -0.299795811 -0.117692721
## neg_22_pd                                      -0.287517173 -0.096423617
## neg_23_pd                                      -0.402605591 -0.116590871
## neg_24_pd                                      -0.522485297 -0.180864324
## neg_25_pd                                      -0.325051268 -0.038788957
## neg_26_pd                                      -0.306147219  0.044238456
## neg_27_pd                                      -0.714236416 -0.213628304
## neg_28_pd                                      -0.669383634 -0.201311333
## neg_29_pd                                      -0.245517266  0.024088229
## neg_30_pd                                      -0.307566464 -0.052096370
## neg_31_pd                                      -0.277751718 -0.054179201
## neg_32_pd                                      -0.179849349  0.032863102
## neg_33_pd                                      -0.216537378  0.109007659
## pos_0_pd                                       -0.119247683 -0.011149669
## pos_1_pd                                       -0.155493502 -0.040113963
## pos_2_pd                                       -0.110679260 -0.005207113
## pos_3_pd                                       -0.149392401 -0.041493922
## pos_4_pd                                       -0.155193824 -0.047126354
## pos_5_pd                                       -0.201852471 -0.092045551
## pos_6_pd                                       -0.211676132 -0.097121035
## pos_7_pd                                       -0.214556474 -0.094523821
## pos_8_pd                                       -0.254768298 -0.140578750
## pos_9_pd                                       -0.281759585 -0.162175014
## pos_10_pd                                      -0.306994676 -0.180016549
## pos_11_pd                                      -0.302439201 -0.183783467
## pos_12_pd                                      -0.317775638 -0.193795841
## pos_13_pd                                      -0.402797602 -0.262320233
## pos_14_pd                                      -0.387924786 -0.247335909
## pos_15_pd                                      -0.387322987 -0.250438341
## pos_16_pd                                      -0.409997721 -0.268524051
## pos_17_pd                                      -0.406130067 -0.263061724
## pos_18_pd                                      -0.400316748 -0.253663735
## pos_19_pd                                      -0.385391286 -0.243608362
## pos_20_pd                                      -0.398712925 -0.247582690
## pos_21_pd                                      -0.434854946 -0.274332431
## pos_22_pd                                      -0.424666741 -0.266749984
## pos_23_pd                                      -0.439924967 -0.273338184
## pos_24_pd                                      -0.487458699 -0.310854938
## pos_25_pd                                      -0.475905008 -0.279921074
## pos_26_pd                                      -0.481207255 -0.289983620
## pos_27_pd                                      -0.541287653 -0.338308822
## pos_28_pd                                      -0.530318463 -0.321207072
## pos_29_pd                                      -0.554552376 -0.334408979
## pos_30_pd                                      -0.513141915 -0.285008197
## pos_31_pd                                      -0.496762582 -0.241545861
## pos_32_pd                                      -0.548421385 -0.298666643
## pos_33_pd                                      -0.636276686 -0.326206090
## pos_34_pd                                      -0.633335400 -0.314276284
## pos_35_pd                                      -0.790567646 -0.514644392
## pos_36_pd                                      -0.805117655 -0.541250661
## pos_37_pd                                      -0.781870141 -0.534987709
## pos_38_pd                                      -0.755748754 -0.513830174
## pos_39_pd                                      -0.844780244 -0.534088308
## s(Time_Period_ID):as.factor(Region)Midwest.1   -0.606300486 -0.494737079
## s(Time_Period_ID):as.factor(Region)Midwest.2   -0.313196740 -0.224608521
## s(Time_Period_ID):as.factor(Region)Midwest.3    0.005273923  0.093175900
## s(Time_Period_ID):as.factor(Region)Midwest.4    0.247618782  0.333711394
## s(Time_Period_ID):as.factor(Region)Midwest.5    0.429783397  0.513087394
## s(Time_Period_ID):as.factor(Region)Midwest.6    0.613350902  0.703168971
## s(Time_Period_ID):as.factor(Region)Midwest.7    0.809910433  0.903771029
## s(Time_Period_ID):as.factor(Region)Midwest.8    1.040189148  1.159056321
## s(Time_Period_ID):as.factor(Region)Midwest.9    0.970869551  1.099762557
## s(Time_Period_ID):as.factor(Region)Northeast.1 -0.858979108 -0.570605352
## s(Time_Period_ID):as.factor(Region)Northeast.2 -0.578240648 -0.351951731
## s(Time_Period_ID):as.factor(Region)Northeast.3  0.135544664  0.305946250
## s(Time_Period_ID):as.factor(Region)Northeast.4  0.069629519  0.234789810
## s(Time_Period_ID):as.factor(Region)Northeast.5  0.184484866  0.353117285
## s(Time_Period_ID):as.factor(Region)Northeast.6  0.438958937  0.593323854
## s(Time_Period_ID):as.factor(Region)Northeast.7  0.822062192  0.987559759
## s(Time_Period_ID):as.factor(Region)Northeast.8  1.245895974  1.424383187
## s(Time_Period_ID):as.factor(Region)Northeast.9  1.039238207  1.204455894
## s(Time_Period_ID):as.factor(Region)South.1     -0.486926536 -0.389922135
## s(Time_Period_ID):as.factor(Region)South.2     -0.208412768 -0.131694247
## s(Time_Period_ID):as.factor(Region)South.3      0.057708791  0.143187991
## s(Time_Period_ID):as.factor(Region)South.4      0.244212376  0.320084111
## s(Time_Period_ID):as.factor(Region)South.5      0.395655933  0.464161403
## s(Time_Period_ID):as.factor(Region)South.6      0.523618590  0.604633801
## s(Time_Period_ID):as.factor(Region)South.7      0.707913502  0.807908570
## s(Time_Period_ID):as.factor(Region)South.8      0.957313615  1.095942563
## s(Time_Period_ID):as.factor(Region)South.9      0.824688260  0.991245595
## s(Time_Period_ID):as.factor(Region)West.1      -0.428787038 -0.308386528
## s(Time_Period_ID):as.factor(Region)West.2      -0.225040794 -0.131400014
## s(Time_Period_ID):as.factor(Region)West.3      -0.021682692  0.069973226
## s(Time_Period_ID):as.factor(Region)West.4       0.154473915  0.230646763
## s(Time_Period_ID):as.factor(Region)West.5       0.275587256  0.357605994
## s(Time_Period_ID):as.factor(Region)West.6       0.374227506  0.468020533
## s(Time_Period_ID):as.factor(Region)West.7       0.467281938  0.566258884
## s(Time_Period_ID):as.factor(Region)West.8       0.602067415  0.719563215
## s(Time_Period_ID):as.factor(Region)West.9       0.585285435  0.704585511
##                                                     ub_coef    sd_coef
## (Intercept)                                    -9.485048518 0.06260554
## StateAlaska                                     0.252854392 0.07711180
## StateArizona                                    0.371278832 0.05785276
## StateArkansas                                  -0.405537177 0.05637846
## StateCalifornia                                 0.072705071 0.06895691
## StateColorado                                   0.126689462 0.06338877
## StateConnecticut                                0.357254117 0.06881462
## StateDelaware                                   0.291485448 0.06960167
## StateFlorida                                    0.533075637 0.06130157
## StateGeorgia                                    0.242851168 0.05879665
## StateHawaii                                    -0.324024904 0.07302326
## StateIdaho                                     -0.041208248 0.06013198
## StateIllinois                                   0.364093459 0.06658763
## StateIndiana                                    0.177104884 0.05154743
## StateIowa                                      -0.514194525 0.05618284
## StateKansas                                    -0.126845056 0.04985086
## StateKentucky                                   0.782971560 0.04896702
## StateLouisiana                                  0.497679858 0.05244459
## StateMaine                                      0.215462271 0.07173696
## StateMaryland                                  -1.228284558 0.11406494
## StateMassachusetts                              0.135877385 0.11170406
## StateMichigan                                   0.147618148 0.04951095
## StateMinnesota                                 -0.535597293 0.05594594
## StateMississippi                                0.036205147 0.06299682
## StateMissouri                                   0.286347363 0.05134940
## StateMontana                                   -0.277892384 0.05940773
## StateNebraska                                  -0.805873041 0.05426610
## StateNevada                                     0.606840670 0.05671821
## StateNew Hampshire                              0.238509408 0.05812118
## StateNew Jersey                                 0.302164511 0.07981467
## StateNew Mexico                                 0.757452480 0.06900101
## StateNew York                                  -0.011901350 0.06141758
## StateNorth Carolina                             0.362703644 0.04504656
## StateNorth Dakota                              -1.001735677 0.08723072
## StateOhio                                       0.724431486 0.06902030
## StateOklahoma                                   0.569948619 0.05960772
## StateOregon                                    -0.181254607 0.06010472
## StatePennsylvania                               0.795477611 0.06021331
## StateRhode Island                              -0.035181348 0.14150317
## StateSouth Carolina                             0.277064316 0.05914811
## StateSouth Dakota                              -0.951424485 0.06808492
## StateTennessee                                  0.559153670 0.04511427
## StateTexas                                      0.208848344 0.06191879
## StateUtah                                       0.133112815 0.09548706
## StateVermont                                   -0.046921170 0.07355433
## StateVirginia                                   0.132113157 0.05138128
## StateWashington                                 0.145143787 0.06176395
## StateWest Virginia                              0.911452242 0.07530100
## StateWisconsin                                  0.152017097 0.04931944
## StateWyoming                                    0.104291768 0.06691529
## Naloxone_Pharmacy_Yes_Redefined                 0.004180259 0.03341835
## Naloxone_Pharmacy_No_Redefined                  0.068573849 0.03326554
## Medical_Marijuana_Redefined                     0.269090156 0.03572430
## Recreational_Marijuana_Redefined               -0.021785870 0.03920442
## GSL_Redefined                                   0.118868012 0.02835945
## PDMP_Redefined                                 -0.122885394 0.02848379
## Medicaid_Expansion_Redefined                    0.141021172 0.02644147
## neg_2_pd                                        0.133399068 0.05758894
## neg_3_pd                                        0.128270046 0.05689435
## neg_4_pd                                        0.110172337 0.06171231
## neg_5_pd                                        0.104286007 0.05984232
## neg_6_pd                                        0.105491968 0.05677068
## neg_7_pd                                        0.072661628 0.05953221
## neg_8_pd                                        0.044136525 0.07381667
## neg_9_pd                                        0.131811558 0.08008699
## neg_10_pd                                       0.166985310 0.07293569
## neg_11_pd                                       0.176196353 0.07693073
## neg_12_pd                                       0.280985250 0.08446414
## neg_13_pd                                       0.183222741 0.08573747
## neg_14_pd                                       0.143302533 0.08113885
## neg_15_pd                                       0.129679111 0.09651757
## neg_16_pd                                       0.129948127 0.08863032
## neg_17_pd                                       0.092943501 0.07810965
## neg_18_pd                                       0.092299794 0.07357546
## neg_19_pd                                       0.077546416 0.11832168
## neg_20_pd                                       0.062267675 0.13060142
## neg_21_pd                                       0.064410369 0.09290974
## neg_22_pd                                       0.094669940 0.09749671
## neg_23_pd                                       0.169423848 0.14592588
## neg_24_pd                                       0.160756649 0.17429641
## neg_25_pd                                       0.247473355 0.14605220
## neg_26_pd                                       0.394624131 0.17876820
## neg_27_pd                                       0.286979809 0.25541230
## neg_28_pd                                       0.266760967 0.23881240
## neg_29_pd                                       0.293693724 0.13755382
## neg_30_pd                                       0.203373723 0.13034188
## neg_31_pd                                       0.169393317 0.11406761
## neg_32_pd                                       0.245575554 0.10852676
## neg_33_pd                                       0.434552697 0.16609441
## pos_0_pd                                        0.096948344 0.05515205
## pos_1_pd                                        0.075265577 0.05886711
## pos_2_pd                                        0.100265034 0.05381232
## pos_3_pd                                        0.066404557 0.05505024
## pos_4_pd                                        0.060941117 0.05513646
## pos_5_pd                                        0.017761369 0.05602394
## pos_6_pd                                        0.017434062 0.05844648
## pos_7_pd                                        0.025508832 0.06124115
## pos_8_pd                                       -0.026389202 0.05825997
## pos_9_pd                                       -0.042590442 0.06101254
## pos_10_pd                                      -0.053038422 0.06478476
## pos_11_pd                                      -0.065127733 0.06053864
## pos_12_pd                                      -0.069816043 0.06325500
## pos_13_pd                                      -0.121842865 0.07167213
## pos_14_pd                                      -0.106747033 0.07172902
## pos_15_pd                                      -0.113553695 0.06983911
## pos_16_pd                                      -0.127050380 0.07218044
## pos_17_pd                                      -0.119993382 0.07299405
## pos_18_pd                                      -0.107010723 0.07482297
## pos_19_pd                                      -0.101825438 0.07233823
## pos_20_pd                                      -0.096452455 0.07710726
## pos_21_pd                                      -0.113809917 0.08189924
## pos_22_pd                                      -0.108833226 0.08056977
## pos_23_pd                                      -0.106751400 0.08499326
## pos_24_pd                                      -0.134251176 0.09010396
## pos_25_pd                                      -0.083937141 0.09999180
## pos_26_pd                                      -0.098759986 0.09756308
## pos_27_pd                                      -0.135329992 0.10356063
## pos_28_pd                                      -0.112095682 0.10668949
## pos_29_pd                                      -0.114265581 0.11231806
## pos_30_pd                                      -0.056874479 0.11639475
## pos_31_pd                                       0.013670859 0.13021261
## pos_32_pd                                      -0.048911901 0.12742589
## pos_33_pd                                      -0.016135493 0.15819928
## pos_34_pd                                       0.004782832 0.16278526
## pos_35_pd                                      -0.238721139 0.14077717
## pos_36_pd                                      -0.277383666 0.13462602
## pos_37_pd                                      -0.288105278 0.12596042
## pos_38_pd                                      -0.271911593 0.12342785
## pos_39_pd                                      -0.223396372 0.15851629
## s(Time_Period_ID):as.factor(Region)Midwest.1   -0.383173672 0.05692011
## s(Time_Period_ID):as.factor(Region)Midwest.2   -0.136020302 0.04519807
## s(Time_Period_ID):as.factor(Region)Midwest.3    0.181077878 0.04484795
## s(Time_Period_ID):as.factor(Region)Midwest.4    0.419804007 0.04392480
## s(Time_Period_ID):as.factor(Region)Midwest.5    0.596391391 0.04250204
## s(Time_Period_ID):as.factor(Region)Midwest.6    0.792987040 0.04582555
## s(Time_Period_ID):as.factor(Region)Midwest.7    0.997631624 0.04788806
## s(Time_Period_ID):as.factor(Region)Midwest.8    1.277923493 0.06064652
## s(Time_Period_ID):as.factor(Region)Midwest.9    1.228655562 0.06576174
## s(Time_Period_ID):as.factor(Region)Northeast.1 -0.282231597 0.14712947
## s(Time_Period_ID):as.factor(Region)Northeast.2 -0.125662815 0.11545353
## s(Time_Period_ID):as.factor(Region)Northeast.3  0.476347836 0.08693958
## s(Time_Period_ID):as.factor(Region)Northeast.4  0.399950100 0.08426545
## s(Time_Period_ID):as.factor(Region)Northeast.5  0.521749703 0.08603695
## s(Time_Period_ID):as.factor(Region)Northeast.6  0.747688772 0.07875761
## s(Time_Period_ID):as.factor(Region)Northeast.7  1.153057326 0.08443753
## s(Time_Period_ID):as.factor(Region)Northeast.8  1.602870399 0.09106490
## s(Time_Period_ID):as.factor(Region)Northeast.9  1.369673580 0.08429474
## s(Time_Period_ID):as.factor(Region)South.1     -0.292917733 0.04949204
## s(Time_Period_ID):as.factor(Region)South.2     -0.054975725 0.03914210
## s(Time_Period_ID):as.factor(Region)South.3      0.228667190 0.04361184
## s(Time_Period_ID):as.factor(Region)South.4      0.395955845 0.03871007
## s(Time_Period_ID):as.factor(Region)South.5      0.532666874 0.03495177
## s(Time_Period_ID):as.factor(Region)South.6      0.685649013 0.04133429
## s(Time_Period_ID):as.factor(Region)South.7      0.907903637 0.05101789
## s(Time_Period_ID):as.factor(Region)South.8      1.234571510 0.07072905
## s(Time_Period_ID):as.factor(Region)South.9      1.157802930 0.08497823
## s(Time_Period_ID):as.factor(Region)West.1      -0.187986018 0.06142883
## s(Time_Period_ID):as.factor(Region)West.2      -0.037759235 0.04777591
## s(Time_Period_ID):as.factor(Region)West.3       0.161629143 0.04676322
## s(Time_Period_ID):as.factor(Region)West.4       0.306819612 0.03886370
## s(Time_Period_ID):as.factor(Region)West.5       0.439624731 0.04184629
## s(Time_Period_ID):as.factor(Region)West.6       0.561813560 0.04785359
## s(Time_Period_ID):as.factor(Region)West.7       0.665235830 0.05049844
## s(Time_Period_ID):as.factor(Region)West.8       0.837059016 0.05994684
## s(Time_Period_ID):as.factor(Region)West.9       0.823885587 0.06086739
# write.csv(format(round(sensitivity_anlys_event_study_sd_and_ci, 3), nsmall = 3), "./Data/event_study_coef_and_ci.csv")

5.3.3 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_event_study_log_smoothed_time <- sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time %>%
  mutate(term = rownames(sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                            function(x){paste("neg_", x, "_pd", sep = "")}), 
                     sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                                 min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                            function(x){paste("pos_", x, "_pd", sep = "")})))
colnames(plot_event_study_log_smoothed_time) <- c("term", "estimate", "conf.low", "conf.high")

dwplot(plot_event_study_log_smoothed_time, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                                 min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                              function(x){paste("pos_", x, "_pd", sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                              function(x){paste("neg_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time Relative to Time of Treatment", 
       x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Pre-Intervention and Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

plot_event_study_log_smoothed_time_paper <- plot_event_study_log_smoothed_time 
plot_event_study_log_smoothed_time_paper$term <- ifelse(grepl("pos_", plot_event_study_log_smoothed_time_paper$term), 
                                                        paste("+", extract_numeric(plot_event_study_log_smoothed_time_paper$term), 
                                                              sep = ""), 
                                                        paste("-", extract_numeric(plot_event_study_log_smoothed_time_paper$term), 
                                                              sep = ""))

# pdf("./Figures/pre_tx_trend_ols_model_5_13_22.pdf")
dwplot(plot_event_study_log_smoothed_time_paper, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                                 min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                              function(x){paste("+", x, sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                              function(x){paste("-", x, sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(size = 5, angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time Relative to Time of Treatment", 
       x = "Coefficients and 95% Confidence Intervals") + 
  scale_color_grey() + 
  coord_flip() +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

# dev.off()

5.4 Analysis With Only Periods After Treatment

formula_post_tx_log_smoothed_time <- formula(paste("log(prop_dead)~ State +
                                           s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                           paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                                                             min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                                                        function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_post_tx_model_log_smoothed_time<-gam(formula_post_tx_log_smoothed_time,
                                                       data = sensitivity_anlys_event_study_data)
# summary(sensitivity_anlys_post_tx_model_log_smoothed_time)

5.4.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_post_tx_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time <- coef(sensitivity_anlys_post_tx_model_log_smoothed_time)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time <- 
  compute_sd_and_CI_linear_link(as.matrix(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time), 
                    log(sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time, 
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time))
# sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time

5.4.2 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_post_tx_log_smoothed_time <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time %>%
  mutate(term = rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                                 min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                            function(x){paste("pos_", x, "_pd", sep = "")}))) 
colnames(plot_post_tx_log_smoothed_time) <- c("term", "estimate", "conf.low", "conf.high")
plot_post_tx_log_smoothed_time$num_states <- sapply(plot_post_tx_log_smoothed_time$term,
                                                    function(x){sum(sensitivity_anlys_event_study_data[,x])})

dwplot(plot_post_tx_log_smoothed_time, colour = "black",
       vars_order =  c(sapply(((max(merged_main_time_data_int$Time_Period_ID) - 
                                  min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0), 
                              function(x){paste("pos_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45, size = 4)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time Periods", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() 

# geom_vline(aes(xintercept = coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"]), linetype = "dashed", color = "red") +
# geom_text(aes(label = paste("Coefficient Estimate: ", coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"]), y = 12, 
#           x = coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"] + 0.1), color = "red")
# geom_text(aes(label = num_states, x = .1, y = 40:1), size = 2)

5.5 Analysis With Linear Effects Periods After Treatment

#use this function to compute the cumulative sum, but resets the sum if the variable was 0
compute_cumsum = function(x){
  cs = cumsum(x)
  cs - cummax((x == 0) * cs)
}
sensitivity_anlys_event_study_data_lin_post_tx <- sensitivity_anlys_event_study_data %>%
  arrange(State, Time_Period_ID) %>%
  group_by(State) %>%
  mutate(sum_tx_periods = pos_0_pd + pos_1_pd + pos_2_pd + pos_3_pd + 
           pos_4_pd + pos_5_pd + pos_6_pd + pos_7_pd + pos_8_pd + pos_9_pd + 
           pos_10_pd + pos_11_pd + pos_12_pd + pos_13_pd + pos_14_pd + 
           pos_15_pd + pos_16_pd + pos_17_pd + pos_18_pd + pos_19_pd + 
           pos_20_pd + pos_21_pd + pos_22_pd + pos_23_pd + pos_24_pd + 
           pos_25_pd + pos_26_pd + pos_27_pd + pos_28_pd + pos_29_pd + 
           pos_30_pd + pos_31_pd + pos_32_pd + pos_33_pd + pos_34_pd + 
           pos_35_pd + pos_36_pd + pos_37_pd + pos_38_pd + pos_39_pd,
         time_after_tx = cumsum(sum_tx_periods),
         num_pd_w_tx = compute_cumsum(Intervention_Redefined ),
         num_pd_w_naloxone_yes = compute_cumsum(Naloxone_Pharmacy_Yes_Redefined),
         num_pd_w_naloxone_no = compute_cumsum(Naloxone_Pharmacy_No_Redefined),
         num_pd_w_med_marijuana = compute_cumsum(Medical_Marijuana_Redefined),
         num_pd_w_rec_marijuana = compute_cumsum(Recreational_Marijuana_Redefined),
         num_pd_w_gsl = compute_cumsum(GSL_Redefined),
         num_pd_w_pdmp = compute_cumsum(PDMP_Redefined),
         num_pd_w_medicaid = compute_cumsum(Medicaid_Expansion_Redefined),
         lag_num_pd_w_tx = lag(num_pd_w_tx),
         lag_num_pd_w_naloxone_yes = lag(num_pd_w_naloxone_yes),
         lag_num_pd_w_naloxone_no = lag(num_pd_w_naloxone_no),
         lag_num_pd_w_med_marijuana = lag(num_pd_w_med_marijuana),
         lag_num_pd_w_rec_marijuana = lag(num_pd_w_rec_marijuana),
         lag_num_pd_w_gsl = lag(num_pd_w_gsl),
         lag_num_pd_w_pdmp = lag(num_pd_w_pdmp),
         lag_num_pd_w_medicaid = lag(num_pd_w_medicaid)) #lag so that intercept = effect when tx first occurs

#fill in a 0 for the NAs so we keep all the data and at most this will be 0
#also fill in a 0 for the linear effect of intervention for the first period

sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_tx[
  sensitivity_anlys_event_study_data_lin_post_tx$Intervention_Redefined < 1] <- 
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_yes[
    sensitivity_anlys_event_study_data_lin_post_tx$Naloxone_Pharmacy_Yes_Redefined < 1]<-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_no[
    sensitivity_anlys_event_study_data_lin_post_tx$Naloxone_Pharmacy_No_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_med_marijuana[
    sensitivity_anlys_event_study_data_lin_post_tx$Medical_Marijuana_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_rec_marijuana[
    sensitivity_anlys_event_study_data_lin_post_tx$Recreational_Marijuana_Redefined < 1] <- 
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_gsl[
    sensitivity_anlys_event_study_data_lin_post_tx$GSL_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_pdmp[
    sensitivity_anlys_event_study_data_lin_post_tx$PDMP_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_medicaid[
    sensitivity_anlys_event_study_data_lin_post_tx$Medicaid_Expansion_Redefined < 1] <-0



#run the gam model
sensitivity_anlys_lin_post_tx_model_log_smoothed_time<-gam(log(prop_dead)~ State +
                                                             s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                             Naloxone_Pharmacy_Yes_Redefined + 
                                                             lag_num_pd_w_naloxone_yes +
                                                             Naloxone_Pharmacy_No_Redefined + 
                                                             lag_num_pd_w_naloxone_no +
                                                             Medical_Marijuana_Redefined + 
                                                             lag_num_pd_w_med_marijuana +
                                                             Recreational_Marijuana_Redefined + 
                                                             lag_num_pd_w_rec_marijuana +
                                                             GSL_Redefined + 
                                                             lag_num_pd_w_gsl +
                                                             PDMP_Redefined + 
                                                             lag_num_pd_w_pdmp +
                                                             Medicaid_Expansion_Redefined +
                                                             lag_num_pd_w_medicaid +
                                                             Intervention_Redefined +
                                                             lag_num_pd_w_tx,
                                                           data = sensitivity_anlys_event_study_data_lin_post_tx)
summary(sensitivity_anlys_lin_post_tx_model_log_smoothed_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + Intervention_Redefined + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.678941   0.062382 -155.157  < 2e-16 ***
## StateAlaska                       0.177343   0.083697    2.119 0.034231 *  
## StateArizona                      0.264886   0.066237    3.999 6.60e-05 ***
## StateArkansas                    -0.502592   0.067334   -7.464 1.27e-13 ***
## StateCalifornia                   0.015999   0.086296    0.185 0.852935    
## StateColorado                     0.077989   0.078766    0.990 0.322234    
## StateConnecticut                  0.263003   0.072140    3.646 0.000274 ***
## StateDelaware                     0.179210   0.068643    2.611 0.009106 ** 
## StateFlorida                      0.469682   0.068570    6.850 9.97e-12 ***
## StateGeorgia                      0.234711   0.070717    3.319 0.000921 ***
## StateHawaii                      -0.412978   0.086070   -4.798 1.73e-06 ***
## StateIdaho                       -0.263980   0.070067   -3.768 0.000170 ***
## StateIllinois                     0.273254   0.074076    3.689 0.000232 ***
## StateIndiana                     -0.016469   0.068716   -0.240 0.810617    
## StateIowa                        -0.613142   0.064686   -9.479  < 2e-16 ***
## StateKansas                      -0.194721   0.064205   -3.033 0.002456 ** 
## StateKentucky                     0.595200   0.069329    8.585  < 2e-16 ***
## StateLouisiana                    0.421595   0.064054    6.582 6.00e-11 ***
## StateMaine                        0.219451   0.083298    2.635 0.008495 ** 
## StateMaryland                    -1.406348   0.070417  -19.972  < 2e-16 ***
## StateMassachusetts               -0.143408   0.069758   -2.056 0.039939 *  
## StateMichigan                     0.008286   0.072491    0.114 0.909009    
## StateMinnesota                   -0.559234   0.070624   -7.918 4.06e-15 ***
## StateMississippi                 -0.130853   0.063894   -2.048 0.040700 *  
## StateMissouri                     0.300737   0.073666    4.082 4.64e-05 ***
## StateMontana                     -0.225004   0.074970   -3.001 0.002724 ** 
## StateNebraska                    -0.903276   0.067623  -13.358  < 2e-16 ***
## StateNevada                       0.535619   0.081555    6.568 6.59e-11 ***
## StateNew Hampshire                0.177163   0.068298    2.594 0.009561 ** 
## StateNew Jersey                   0.227583   0.067883    3.353 0.000816 ***
## StateNew Mexico                   0.684644   0.078563    8.715  < 2e-16 ***
## StateNew York                    -0.220508   0.070836   -3.113 0.001880 ** 
## StateNorth Carolina               0.307306   0.063646    4.828 1.49e-06 ***
## StateNorth Dakota                -1.208449   0.064240  -18.811  < 2e-16 ***
## StateOhio                         0.617865   0.068359    9.039  < 2e-16 ***
## StateOklahoma                     0.367308   0.069080    5.317 1.18e-07 ***
## StateOregon                      -0.092079   0.081816   -1.125 0.260549    
## StatePennsylvania                 0.639060   0.072385    8.829  < 2e-16 ***
## StateRhode Island                -0.312299   0.074771   -4.177 3.09e-05 ***
## StateSouth Carolina               0.126318   0.064639    1.954 0.050825 .  
## StateSouth Dakota                -1.077450   0.067181  -16.038  < 2e-16 ***
## StateTennessee                    0.455862   0.063531    7.175 1.03e-12 ***
## StateTexas                        0.065809   0.071916    0.915 0.360265    
## StateUtah                        -0.115434   0.069154   -1.669 0.095236 .  
## StateVermont                     -0.091676   0.070121   -1.307 0.191236    
## StateVirginia                     0.018175   0.065608    0.277 0.781792    
## StateWashington                   0.107278   0.080482    1.333 0.182707    
## StateWest Virginia                0.656607   0.069869    9.398  < 2e-16 ***
## StateWisconsin                    0.106853   0.066196    1.614 0.106655    
## StateWyoming                     -0.069148   0.063912   -1.082 0.279425    
## Naloxone_Pharmacy_Yes_Redefined  -0.040959   0.041733   -0.981 0.326494    
## lag_num_pd_w_naloxone_yes        -0.027003   0.006588   -4.099 4.33e-05 ***
## Naloxone_Pharmacy_No_Redefined    0.129651   0.043251    2.998 0.002756 ** 
## lag_num_pd_w_naloxone_no         -0.022777   0.004517   -5.043 5.03e-07 ***
## Medical_Marijuana_Redefined       0.192279   0.030305    6.345 2.78e-10 ***
## lag_num_pd_w_med_marijuana       -0.008505   0.002213   -3.843 0.000125 ***
## Recreational_Marijuana_Redefined -0.015978   0.061129   -0.261 0.793829    
## lag_num_pd_w_rec_marijuana       -0.008431   0.010880   -0.775 0.438514    
## GSL_Redefined                     0.071508   0.032779    2.182 0.029267 *  
## lag_num_pd_w_gsl                  0.010557   0.004060    2.600 0.009388 ** 
## PDMP_Redefined                   -0.128942   0.027146   -4.750 2.19e-06 ***
## lag_num_pd_w_pdmp                 0.006260   0.002417    2.590 0.009666 ** 
## Medicaid_Expansion_Redefined      0.076951   0.033493    2.298 0.021699 *  
## lag_num_pd_w_medicaid             0.012902   0.004989    2.586 0.009776 ** 
## Intervention_Redefined           -0.044881   0.023857   -1.881 0.060090 .  
## lag_num_pd_w_tx                  -0.013751   0.001782   -7.715 1.94e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.133  5.113 97.54  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.398  8.897 49.05  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.219  7.377 55.25  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      4.386  5.424 37.09  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.852   Deviance explained = 85.8%
## GCV =  0.082  Scale est. = 0.078309  n = 1980
plot(sensitivity_anlys_lin_post_tx_model_log_smoothed_time, pages = 1)

5.5.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_lin_post_tx_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_lin_post_tx_log_smoothed_time <- coef(sensitivity_anlys_lin_post_tx_model_log_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time_w_cov <- 
  compute_sd_and_CI_linear_link(as.matrix(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time), 
                    log(sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_lin_post_tx_log_smoothed_time, 
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time),
                    print_full_cov = TRUE)
# format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time, 3), nsmall = 3)

sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time <- sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time_w_cov[[1]]
colnames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high", "sd")
sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term <- rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention") + 
  scale_color_grey() + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_log_all_yr <- sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,]
table_of_RR_lin_eff_log_all_yr$estimate <- round(exp(table_of_RR_lin_eff_log_all_yr$estimate), 4)
table_of_RR_lin_eff_log_all_yr$conf.low <- exp(table_of_RR_lin_eff_log_all_yr$conf.low)
table_of_RR_lin_eff_log_all_yr$conf.high <- exp(table_of_RR_lin_eff_log_all_yr$conf.high)
table_of_RR_lin_eff_log_all_yr$ci_95 <- paste("95% CI = (", 
                                              format(round(table_of_RR_lin_eff_log_all_yr$conf.low, 4), nsmall = 4), ", ", 
                                              format(round(table_of_RR_lin_eff_log_all_yr$conf.high, 4), nsmall = 4), ")", sep = "")

# write.csv(table_of_RR_lin_eff_log_all_yr, "./Data/table_of_RR_lin_eff_log_all_yr_5_13_22.csv")
table_of_RR_lin_eff_log_all_yr
##                                   conf.low estimate conf.high          sd
## Naloxone_Pharmacy_Yes_Redefined  0.8187863   0.9599 1.1252597 0.081108684
## lag_num_pd_w_naloxone_yes        0.9475055   0.9734 0.9999156 0.013734234
## Naloxone_Pharmacy_No_Redefined   0.9098115   1.1384 1.4244976 0.114371692
## lag_num_pd_w_naloxone_no         0.9590309   0.9775 0.9962848 0.009721913
## Medical_Marijuana_Redefined      1.0112344   1.2120 1.4526453 0.092401666
## lag_num_pd_w_med_marijuana       0.9783527   0.9915 1.0048868 0.006826499
## Recreational_Marijuana_Redefined 0.7995336   0.9841 1.2113932 0.105994347
## lag_num_pd_w_rec_marijuana       0.9538079   0.9916 1.0308994 0.019827701
## GSL_Redefined                    0.9396939   1.0741 1.2277916 0.068218936
## lag_num_pd_w_gsl                 0.9898149   1.0106 1.0318482 0.010609422
## PDMP_Redefined                   0.7515220   0.8790 1.0281596 0.079955398
## lag_num_pd_w_pdmp                0.9893996   1.0063 1.0234486 0.008631352
## Medicaid_Expansion_Redefined     0.9273050   1.0800 1.2578132 0.077767186
## lag_num_pd_w_medicaid            0.9883064   1.0130 1.0382815 0.012584032
## Intervention_Redefined           0.8235221   0.9561 1.1100472 0.076165143
## lag_num_pd_w_tx                  0.9734497   0.9863 0.9994067 0.006713178
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## Intervention_Redefined                     Intervention_Redefined
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                      ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.8188, 1.1253)
## lag_num_pd_w_naloxone_yes        95% CI = (0.9475, 0.9999)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.9098, 1.4245)
## lag_num_pd_w_naloxone_no         95% CI = (0.9590, 0.9963)
## Medical_Marijuana_Redefined      95% CI = (1.0112, 1.4526)
## lag_num_pd_w_med_marijuana       95% CI = (0.9784, 1.0049)
## Recreational_Marijuana_Redefined 95% CI = (0.7995, 1.2114)
## lag_num_pd_w_rec_marijuana       95% CI = (0.9538, 1.0309)
## GSL_Redefined                    95% CI = (0.9397, 1.2278)
## lag_num_pd_w_gsl                 95% CI = (0.9898, 1.0318)
## PDMP_Redefined                   95% CI = (0.7515, 1.0282)
## lag_num_pd_w_pdmp                95% CI = (0.9894, 1.0234)
## Medicaid_Expansion_Redefined     95% CI = (0.9273, 1.2578)
## lag_num_pd_w_medicaid            95% CI = (0.9883, 1.0383)
## Intervention_Redefined           95% CI = (0.8235, 1.1100)
## lag_num_pd_w_tx                  95% CI = (0.9734, 0.9994)

5.5.2 Attributable Deaths

date_data <- sensitivity_anlys_event_study_data_lin_post_tx[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
var_cov_mat_beta_lin_tx <- as.matrix(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time_w_cov[[2]][
  c("Intervention_Redefined", "lag_num_pd_w_tx"), c("Intervention_Redefined", "lag_num_pd_w_tx")])


attr_deaths_est_log_smoothed_time_lin_post <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx,
                                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time,
                                                                 tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"),
                                                                 var_cov_mat_beta_lin_tx)

attr_deaths_est_log_smoothed_time_lin_post <- merge(attr_deaths_est_log_smoothed_time_lin_post, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_log_smoothed_time_lin_post_summary <- attr_deaths_est_log_smoothed_time_lin_post %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_attr_deaths = sum(attr_deaths),
            total_attr_deaths_lb = sum(attr_deaths_lb),
            total_attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths)
## [1] 197297.8
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths_lb)
## [1] -73711.92
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths_ub)
## [1] 468307.4
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths)/sum(main_analysis_data$imputed_deaths)
## [1] 0.2997463
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths_lb)/sum(main_analysis_data$imputed_deaths)
## [1] -0.1119875
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$total_attr_deaths_ub)/sum(main_analysis_data$imputed_deaths)
## [1] 0.71148
ggplot(attr_deaths_est_log_smoothed_time_lin_post_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year,
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

#overall national overdose deaths
national_od <- sensitivity_anlys_event_study_data_lin_post_tx %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_od = sum(imputed_deaths),
            total_od_prob = sum(imputed_deaths)/sum(population),
            total_pop = sum(population))


national_od <- merge(national_od, attr_deaths_est_log_smoothed_time_lin_post_summary, by = "year")

ggplot(national_od, aes(x = year)) + 
  geom_line(aes(y = total_od, color = "Oberved OD")) +
  geom_line(aes(y = total_attr_deaths, color = "Lives Saved", linetype = "Estimate")) + 
  geom_line(aes(y = total_attr_deaths_lb, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths_ub, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths + total_od, 
                color = "Potential Number of Deaths Had There Not Been DIH Prosecutions")) + 
  labs(x = "Date", y = "People",
       title = "Number of OD Deaths and Estimated Number of Lives Saved in Each Year",
       color = "", linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))

pdf("./Figures/lives_saved_ols_lin_tx_all_time_5_13_22.pdf")
ggplot(attr_deaths_est_log_smoothed_time_lin_post_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Year", y = "Lives Saved",
       # title = "Estimated Number of Lives Saved Per Year,
       # Using Smoothed Time Effects, 
       # Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = c(.3, .8)) + 
  scale_linetype_manual(values = c("dashed", "solid"))
dev.off()
## quartz_off_screen 
##                 2
pdf("./Figures/lives_saved_and_est_od_ols_lin_tx_all_time_5_13_22.pdf")
ggplot(national_od, aes(x = year)) + 
  geom_line(aes(y = total_od, color = "Oberved OD")) +
  geom_line(aes(y = total_attr_deaths, color = "Lives Saved", linetype = "Estimate")) + 
  geom_line(aes(y = total_attr_deaths_lb, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths_ub, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths + total_od, 
                color = "Potential Number of Deaths Had There Not Been DIH Prosecutions")) + 
  labs(x = "Date", y = "People",
       # title = "Number of OD Deaths and Estimated Number of Lives Saved in Each Year",
       color = "", linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))

dev.off()
## quartz_off_screen 
##                 2

5.6 Analysis With Only Periods After Treatment Subset Periods

#run the gam model
data_subset <- sensitivity_anlys_event_study_data_lin_post_tx[sensitivity_anlys_event_study_data_lin_post_tx$Time_Period_ID <= 30,]
sensitivity_anlys_post_tx_model_log_smoothed_time_subset<-gam(log(prop_dead)~ State +
                                                                s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                Naloxone_Pharmacy_Yes_Redefined + 
                                                                lag_num_pd_w_naloxone_yes +
                                                                Naloxone_Pharmacy_No_Redefined + 
                                                                lag_num_pd_w_naloxone_no +
                                                                Medical_Marijuana_Redefined + 
                                                                lag_num_pd_w_med_marijuana +
                                                                Recreational_Marijuana_Redefined + 
                                                                lag_num_pd_w_rec_marijuana +
                                                                GSL_Redefined + 
                                                                lag_num_pd_w_gsl +
                                                                PDMP_Redefined + 
                                                                lag_num_pd_w_pdmp +
                                                                Medicaid_Expansion_Redefined +
                                                                lag_num_pd_w_medicaid +
                                                                Intervention_Redefined +
                                                                lag_num_pd_w_tx,
                                                              data = data_subset)
summary(sensitivity_anlys_post_tx_model_log_smoothed_time_subset)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + Intervention_Redefined + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.951459   0.060807 -163.656  < 2e-16 ***
## StateAlaska                       0.392796   0.095840    4.098 4.40e-05 ***
## StateArizona                      0.392792   0.074735    5.256 1.70e-07 ***
## StateArkansas                    -0.440739   0.076208   -5.783 9.02e-09 ***
## StateCalifornia                   0.172975   0.101297    1.708 0.087934 .  
## StateColorado                     0.223376   0.090539    2.467 0.013738 *  
## StateConnecticut                  0.441991   0.085610    5.163 2.78e-07 ***
## StateDelaware                     0.157065   0.076923    2.042 0.041353 *  
## StateFlorida                      0.554720   0.078904    7.030 3.21e-12 ***
## StateGeorgia                      0.296002   0.081281    3.642 0.000281 ***
## StateHawaii                      -0.311664   0.097704   -3.190 0.001455 ** 
## StateIdaho                       -0.236372   0.080656   -2.931 0.003438 ** 
## StateIllinois                     0.339470   0.086098    3.943 8.45e-05 ***
## StateIndiana                     -0.108572   0.079585   -1.364 0.172716    
## StateIowa                        -0.605676   0.073617   -8.227 4.33e-16 ***
## StateKansas                      -0.130961   0.073781   -1.775 0.076115 .  
## StateKentucky                     0.610785   0.079973    7.637 4.09e-14 ***
## StateLouisiana                    0.448008   0.073978    6.056 1.79e-09 ***
## StateMaine                        0.155585   0.094019    1.655 0.098186 .  
## StateMaryland                    -1.638794   0.080100  -20.459  < 2e-16 ***
## StateMassachusetts               -0.360711   0.080094   -4.504 7.24e-06 ***
## StateMichigan                    -0.042496   0.081743   -0.520 0.603232    
## StateMinnesota                   -0.532398   0.081064   -6.568 7.19e-11 ***
## StateMississippi                 -0.013260   0.073783   -0.180 0.857398    
## StateMissouri                     0.304148   0.078774    3.861 0.000118 ***
## StateMontana                     -0.106784   0.083986   -1.271 0.203782    
## StateNebraska                    -0.829029   0.076434  -10.846  < 2e-16 ***
## StateNevada                       0.662710   0.093110    7.117 1.75e-12 ***
## StateNew Hampshire                0.107350   0.076936    1.395 0.163143    
## StateNew Jersey                   0.182514   0.077227    2.363 0.018247 *  
## StateNew Mexico                   1.077690   0.096046   11.221  < 2e-16 ***
## StateNew York                    -0.168394   0.083201   -2.024 0.043165 *  
## StateNorth Carolina               0.295525   0.073589    4.016 6.24e-05 ***
## StateNorth Dakota                -1.256486   0.073478  -17.100  < 2e-16 ***
## StateOhio                         0.565952   0.078551    7.205 9.47e-13 ***
## StateOklahoma                     0.431977   0.079705    5.420 7.02e-08 ***
## StateOregon                       0.071640   0.095157    0.753 0.451658    
## StatePennsylvania                 0.604419   0.083834    7.210 9.15e-13 ***
## StateRhode Island                -0.550541   0.085376   -6.448 1.55e-10 ***
## StateSouth Carolina               0.184131   0.073997    2.488 0.012949 *  
## StateSouth Dakota                -1.053738   0.076096  -13.848  < 2e-16 ***
## StateTennessee                    0.418373   0.073630    5.682 1.62e-08 ***
## StateTexas                        0.127799   0.083031    1.539 0.123988    
## StateUtah                        -0.209871   0.079834   -2.629 0.008662 ** 
## StateVermont                     -0.097112   0.080309   -1.209 0.226777    
## StateVirginia                    -0.028416   0.075970   -0.374 0.708434    
## StateWashington                   0.341978   0.093606    3.653 0.000268 ***
## StateWest Virginia                0.648490   0.080192    8.087 1.32e-15 ***
## StateWisconsin                    0.090454   0.076056    1.189 0.234524    
## StateWyoming                     -0.015496   0.073536   -0.211 0.833126    
## Naloxone_Pharmacy_Yes_Redefined  -0.178532   0.075409   -2.368 0.018043 *  
## lag_num_pd_w_naloxone_yes        -0.058586   0.023102   -2.536 0.011322 *  
## Naloxone_Pharmacy_No_Redefined    0.150296   0.055069    2.729 0.006428 ** 
## lag_num_pd_w_naloxone_no         -0.044257   0.005736   -7.716 2.28e-14 ***
## Medical_Marijuana_Redefined       0.234565   0.039668    5.913 4.21e-09 ***
## lag_num_pd_w_med_marijuana       -0.016004   0.003107   -5.151 2.96e-07 ***
## Recreational_Marijuana_Redefined -0.249591   0.189149   -1.320 0.187202    
## lag_num_pd_w_rec_marijuana        0.014694   0.090247    0.163 0.870683    
## GSL_Redefined                     0.134755   0.054771    2.460 0.014000 *  
## lag_num_pd_w_gsl                  0.009686   0.009938    0.975 0.329893    
## PDMP_Redefined                   -0.123013   0.029478   -4.173 3.19e-05 ***
## lag_num_pd_w_pdmp                 0.010963   0.002801    3.914 9.51e-05 ***
## Medicaid_Expansion_Redefined      0.033904   0.050582    0.670 0.502791    
## lag_num_pd_w_medicaid             0.016100   0.014929    1.078 0.281019    
## Intervention_Redefined           -0.004995   0.027512   -0.182 0.855964    
## lag_num_pd_w_tx                  -0.016953   0.002425   -6.990 4.26e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   2.641  3.301 121.06  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 7.646  8.531  37.71  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     2.966  3.697  84.05  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      2.863  3.578  49.98  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.832   Deviance explained = 84.1%
## GCV = 0.083767  Scale est. = 0.079119  n = 1480

5.6.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset <-
  data.frame(predict(sensitivity_anlys_post_tx_model_log_smoothed_time_subset, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time_subset <- coef(sensitivity_anlys_post_tx_model_log_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset_w_cov <- 
  compute_sd_and_CI_linear_link(as.matrix(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset), 
                    log(data_subset$prop_dead),
                    coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time_subset,
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset),
                    print_full_cov = TRUE)
# sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset_w_cov[[1]]
colnames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset) <- c("conf.low", "estimate", "conf.high", "sd")
sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_log_all_yr_subset <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,]
table_of_RR_lin_eff_log_all_yr_subset$estimate <- round(exp(table_of_RR_lin_eff_log_all_yr_subset$estimate), 3)
table_of_RR_lin_eff_log_all_yr_subset$conf.low <- exp(table_of_RR_lin_eff_log_all_yr_subset$conf.low)
table_of_RR_lin_eff_log_all_yr_subset$conf.high <- exp(table_of_RR_lin_eff_log_all_yr_subset$conf.high)
table_of_RR_lin_eff_log_all_yr_subset$ci_95 <- paste("95% CI = (", 
                                                     format(round(table_of_RR_lin_eff_log_all_yr_subset$conf.low, 3), nsmall = 3), ", ", 
                                                     format(round(table_of_RR_lin_eff_log_all_yr_subset$conf.high, 3), nsmall = 3), ")",
                                                     sep = "")

# write.csv(table_of_RR_lin_eff_log_all_yr_subset, "./Data/table_of_RR_lin_eff_log_all_yr_subset_5_13_22.csv")
table_of_RR_lin_eff_log_all_yr_subset
##                                   conf.low estimate conf.high          sd
## Naloxone_Pharmacy_Yes_Redefined  0.6631265    0.836 1.0551939 0.118498452
## lag_num_pd_w_naloxone_yes        0.8849338    0.943 1.0050832 0.032477728
## Naloxone_Pharmacy_No_Redefined   0.8467222    1.162 1.5951617 0.161570850
## lag_num_pd_w_naloxone_no         0.9322543    0.957 0.9818023 0.013210280
## Medical_Marijuana_Redefined      0.9875543    1.264 1.6187505 0.126065909
## lag_num_pd_w_med_marijuana       0.9678008    0.984 1.0007205 0.008532959
## Recreational_Marijuana_Redefined 0.5177888    0.779 1.1723436 0.208467505
## lag_num_pd_w_rec_marijuana       0.8761929    1.015 1.1753395 0.074930112
## GSL_Redefined                    0.9001522    1.144 1.4545583 0.122421870
## lag_num_pd_w_gsl                 0.9714048    1.010 1.0495745 0.019744107
## PDMP_Redefined                   0.7548766    0.884 1.0358000 0.080707939
## lag_num_pd_w_pdmp                0.9925185    1.011 1.0298725 0.009424631
## Medicaid_Expansion_Redefined     0.8467866    1.034 1.2637889 0.102148186
## lag_num_pd_w_medicaid            0.9574030    1.016 1.0786728 0.030424048
## Intervention_Redefined           0.8428284    0.995 1.1746876 0.084692380
## lag_num_pd_w_tx                  0.9694544    0.983 0.9971206 0.007178135
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## Intervention_Redefined                     Intervention_Redefined
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                    ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.663, 1.055)
## lag_num_pd_w_naloxone_yes        95% CI = (0.885, 1.005)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.847, 1.595)
## lag_num_pd_w_naloxone_no         95% CI = (0.932, 0.982)
## Medical_Marijuana_Redefined      95% CI = (0.988, 1.619)
## lag_num_pd_w_med_marijuana       95% CI = (0.968, 1.001)
## Recreational_Marijuana_Redefined 95% CI = (0.518, 1.172)
## lag_num_pd_w_rec_marijuana       95% CI = (0.876, 1.175)
## GSL_Redefined                    95% CI = (0.900, 1.455)
## lag_num_pd_w_gsl                 95% CI = (0.971, 1.050)
## PDMP_Redefined                   95% CI = (0.755, 1.036)
## lag_num_pd_w_pdmp                95% CI = (0.993, 1.030)
## Medicaid_Expansion_Redefined     95% CI = (0.847, 1.264)
## lag_num_pd_w_medicaid            95% CI = (0.957, 1.079)
## Intervention_Redefined           95% CI = (0.843, 1.175)
## lag_num_pd_w_tx                  95% CI = (0.969, 0.997)

5.6.2 Attributable Deaths

date_data_subset <- data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset <- date_data_subset[!duplicated(date_data_subset),]
var_cov_mat_beta_lin_tx_subset <- as.matrix(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset_w_cov[[2]][
  c("Intervention_Redefined", "lag_num_pd_w_tx"), c("Intervention_Redefined", "lag_num_pd_w_tx")])
attr_deaths_est_log_smoothed_time_lin_post_subset <- attr_death_compute(data_subset,
                                                                        sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset, 
                                                                        tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"),
                                                                        var_cov_mat_beta_lin_tx_subset)
attr_deaths_est_log_smoothed_time_lin_post_subset <- merge(attr_deaths_est_log_smoothed_time_lin_post_subset, date_data_subset, 
                                                           by.x = "Time_Period", by.y = "Time_Period_ID")


attr_deaths_est_log_smoothed_time_lin_post_subset_summary <- attr_deaths_est_log_smoothed_time_lin_post_subset %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_attr_deaths = sum(attr_deaths),
            total_attr_deaths_lb = sum(attr_deaths_lb),
            total_attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths)
## [1] 67149.1
sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths_lb)
## [1] -31463.76
sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths_ub)
## [1] 165762
sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths)/sum(data_subset$imputed_deaths)
## [1] 0.1771394
sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths_lb)/sum(data_subset$imputed_deaths)
## [1] -0.08300144
sum(attr_deaths_est_log_smoothed_time_lin_post_subset_summary$total_attr_deaths_ub)/sum(data_subset$imputed_deaths)
## [1] 0.4372803
ggplot(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year 
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

ggplot() + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths, linetype = "Estimate",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_lb, linetype = "95% CI",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_ub, linetype = "95% CI",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths, linetype = "Estimate",
                          color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths_lb, 
                          linetype = "95% CI",
                          color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths_ub, 
                          linetype = "95% CI",
                          color = "Subset Dataset")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "", color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

5.7 Analysis With Linear Effects Periods After Treatment Logistic Regression

#run the gam model
sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time<-gam(cbind(round(imputed_deaths), round(num_alive))~ State +
                                                                  s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                  Naloxone_Pharmacy_Yes_Redefined + 
                                                                  lag_num_pd_w_naloxone_yes +
                                                                  Naloxone_Pharmacy_No_Redefined + 
                                                                  lag_num_pd_w_naloxone_no +
                                                                  Medical_Marijuana_Redefined + 
                                                                  lag_num_pd_w_med_marijuana +
                                                                  Recreational_Marijuana_Redefined + 
                                                                  lag_num_pd_w_rec_marijuana +
                                                                  GSL_Redefined + 
                                                                  lag_num_pd_w_gsl +
                                                                  PDMP_Redefined + 
                                                                  lag_num_pd_w_pdmp +
                                                                  Medicaid_Expansion_Redefined +
                                                                  lag_num_pd_w_medicaid +
                                                                  Intervention_Redefined +
                                                                  lag_num_pd_w_tx,
                                                                data = sensitivity_anlys_event_study_data_lin_post_tx,
                                                                family = "binomial")
summary(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time)
## 
## Family: binomial 
## Link function: logit 
## 
## Formula:
## cbind(round(imputed_deaths), round(num_alive)) ~ State + s(Time_Period_ID, 
##     bs = "cr", by = as.factor(Region)) + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + Intervention_Redefined + lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)                      -9.6918398  0.0165286 -586.368  < 2e-16 ***
## StateAlaska                       0.1660062  0.0316907    5.238 1.62e-07 ***
## StateArizona                      0.2470040  0.0148999   16.578  < 2e-16 ***
## StateArkansas                    -0.4514581  0.0211955  -21.300  < 2e-16 ***
## StateCalifornia                   0.0115038  0.0191547    0.601 0.548126    
## StateColorado                     0.1212188  0.0196259    6.176 6.56e-10 ***
## StateConnecticut                  0.0859484  0.0173084    4.966 6.85e-07 ***
## StateDelaware                     0.2921966  0.0245539   11.900  < 2e-16 ***
## StateFlorida                      0.4469502  0.0148057   30.188  < 2e-16 ***
## StateGeorgia                      0.2005269  0.0169730   11.814  < 2e-16 ***
## StateHawaii                      -0.3867800  0.0302216  -12.798  < 2e-16 ***
## StateIdaho                       -0.3168152  0.0254657  -12.441  < 2e-16 ***
## StateIllinois                     0.1658041  0.0165498   10.018  < 2e-16 ***
## StateIndiana                     -0.0491962  0.0157645   -3.121 0.001804 ** 
## StateIowa                        -0.6671034  0.0219000  -30.461  < 2e-16 ***
## StateKansas                      -0.3062736  0.0197456  -15.511  < 2e-16 ***
## StateKentucky                     0.5188091  0.0154340   33.615  < 2e-16 ***
## StateLouisiana                    0.4010566  0.0149643   26.801  < 2e-16 ***
## StateMaine                        0.2586864  0.0263564    9.815  < 2e-16 ***
## StateMaryland                    -0.9568958  0.0217895  -43.915  < 2e-16 ***
## StateMassachusetts                0.1586346  0.0151818   10.449  < 2e-16 ***
## StateMichigan                     0.0130877  0.0162585    0.805 0.420834    
## StateMinnesota                   -0.5908795  0.0197106  -29.978  < 2e-16 ***
## StateMississippi                 -0.2354073  0.0188147  -12.512  < 2e-16 ***
## StateMissouri                     0.3211547  0.0188890   17.002  < 2e-16 ***
## StateMontana                     -0.1494454  0.0313434   -4.768 1.86e-06 ***
## StateNebraska                    -0.9918381  0.0296070  -33.500  < 2e-16 ***
## StateNevada                       0.5342117  0.0205026   26.056  < 2e-16 ***
## StateNew Hampshire                0.2682052  0.0215241   12.461  < 2e-16 ***
## StateNew Jersey                   0.2753953  0.0154320   17.846  < 2e-16 ***
## StateNew Mexico                   0.5017865  0.0195363   25.685  < 2e-16 ***
## StateNew York                    -0.3234978  0.0143800  -22.496  < 2e-16 ***
## StateNorth Carolina               0.2718936  0.0132117   20.580  < 2e-16 ***
## StateNorth Dakota                -1.1953770  0.0457733  -26.115  < 2e-16 ***
## StateOhio                         0.6679880  0.0152538   43.792  < 2e-16 ***
## StateOklahoma                     0.2955589  0.0165172   17.894  < 2e-16 ***
## StateOregon                      -0.0682414  0.0223395   -3.055 0.002253 ** 
## StatePennsylvania                 0.5992187  0.0159711   37.519  < 2e-16 ***
## StateRhode Island                 0.1401910  0.0245331    5.714 1.10e-08 ***
## StateSouth Carolina               0.0672272  0.0160186    4.197 2.71e-05 ***
## StateSouth Dakota                -1.1228077  0.0435739  -25.768  < 2e-16 ***
## StateTennessee                    0.4346807  0.0133160   32.643  < 2e-16 ***
## StateTexas                       -0.0454967  0.0159415   -2.854 0.004317 ** 
## StateUtah                         0.0780823  0.0196717    3.969 7.21e-05 ***
## StateVermont                     -0.1154520  0.0325629   -3.546 0.000392 ***
## StateVirginia                    -0.0106755  0.0150892   -0.707 0.479259    
## StateWashington                   0.1034138  0.0192418    5.374 7.68e-08 ***
## StateWest Virginia                0.6933803  0.0173454   39.975  < 2e-16 ***
## StateWisconsin                    0.0702017  0.0158598    4.426 9.58e-06 ***
## StateWyoming                     -0.0314449  0.0337961   -0.930 0.352148    
## Naloxone_Pharmacy_Yes_Redefined   0.0155747  0.0080655    1.931 0.053480 .  
## lag_num_pd_w_naloxone_yes        -0.0213838  0.0011158  -19.165  < 2e-16 ***
## Naloxone_Pharmacy_No_Redefined    0.0619851  0.0079301    7.816 5.43e-15 ***
## lag_num_pd_w_naloxone_no         -0.0021388  0.0009269   -2.307 0.021031 *  
## Medical_Marijuana_Redefined       0.0335407  0.0058322    5.751 8.87e-09 ***
## lag_num_pd_w_med_marijuana       -0.0038222  0.0006421   -5.953 2.64e-09 ***
## Recreational_Marijuana_Redefined  0.0018514  0.0101063    0.183 0.854649    
## lag_num_pd_w_rec_marijuana       -0.0199844  0.0019355  -10.325  < 2e-16 ***
## GSL_Redefined                     0.0256909  0.0060998    4.212 2.53e-05 ***
## lag_num_pd_w_gsl                  0.0135896  0.0008501   15.987  < 2e-16 ***
## PDMP_Redefined                   -0.0327741  0.0068817   -4.762 1.91e-06 ***
## lag_num_pd_w_pdmp                 0.0046873  0.0006373    7.355 1.91e-13 ***
## Medicaid_Expansion_Redefined      0.0676747  0.0067899    9.967  < 2e-16 ***
## lag_num_pd_w_medicaid             0.0142307  0.0010244   13.892  < 2e-16 ***
## Intervention_Redefined           -0.0187494  0.0063105   -2.971 0.002967 ** 
## lag_num_pd_w_tx                  -0.0148682  0.0005601  -26.544  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df Chi.sq p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   8.923  8.998   4729  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.964  9.000   4285  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     8.937  8.999   4379  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      8.775  8.984   2117  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.917   Deviance explained = 91.2%
## UBRE = 7.8233  Scale est. = 1         n = 1980
plot(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time, pages = 1)

5.7.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_logistic_smoothed_time <-
  data.frame(predict(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_lin_post_tx_logistic_smoothed_time <- coef(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time_w_cov <- 
  compute_sd_and_CI_logistic(as.matrix(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_logistic_smoothed_time), 
                    (sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_lin_post_tx_logistic_smoothed_time, 
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_logistic_smoothed_time),
                    print_full_cov = TRUE)
# format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time, 3), nsmall = 3)

sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time <- sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time_w_cov[[1]]

colnames(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time) <- c("conf.low", "estimate", "conf.high", "exp_lb",
                                                                              "exp_coef", "exp_ub", "sd")
sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term <- rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention") + 
  scale_color_grey() + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_logistic_all_yr <- sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time[51:66,]
table_of_RR_lin_eff_logistic_all_yr$estimate <- round(exp(table_of_RR_lin_eff_logistic_all_yr$estimate), 3)
table_of_RR_lin_eff_logistic_all_yr$conf.low <- exp(table_of_RR_lin_eff_logistic_all_yr$conf.low)
table_of_RR_lin_eff_logistic_all_yr$conf.high <- exp(table_of_RR_lin_eff_logistic_all_yr$conf.high)
table_of_RR_lin_eff_logistic_all_yr$ci_95 <- paste("95% CI = (", 
                                                   format(round(table_of_RR_lin_eff_logistic_all_yr$conf.low, 3), nsmall = 3), ", ", 
                                                   format(round(table_of_RR_lin_eff_logistic_all_yr$conf.high, 3), nsmall = 3), ")", 
                                                   sep = "")

# write.csv(table_of_RR_lin_eff_logistic_all_yr, "./Data/table_of_RR_lin_eff_logistic_all_yr_5_13_22.csv")
table_of_RR_lin_eff_logistic_all_yr
##                                   conf.low estimate conf.high    exp_lb
## Naloxone_Pharmacy_Yes_Redefined  0.8731089    1.016  1.181570 0.8731089
## lag_num_pd_w_naloxone_yes        0.9561626    0.979  1.002062 0.9561626
## Naloxone_Pharmacy_No_Redefined   0.8912458    1.064  1.270112 0.8912458
## lag_num_pd_w_naloxone_no         0.9810328    0.998  1.014983 0.9810328
## Medical_Marijuana_Redefined      0.8811402    1.034  1.213635 0.8811402
## lag_num_pd_w_med_marijuana       0.9806591    0.996  1.011957 0.9806591
## Recreational_Marijuana_Redefined 0.8578127    1.002  1.170080 0.8578127
## lag_num_pd_w_rec_marijuana       0.9493872    0.980  1.012042 0.9493872
## GSL_Redefined                    0.9106076    1.026  1.156069 0.9106076
## lag_num_pd_w_gsl                 0.9935669    1.014  1.034205 0.9935669
## PDMP_Redefined                   0.8171409    0.968  1.146135 0.8171409
## lag_num_pd_w_pdmp                0.9832284    1.005  1.026637 0.9832284
## Medicaid_Expansion_Redefined     0.9529083    1.070  1.201518 0.9529083
## lag_num_pd_w_medicaid            0.9949598    1.014  1.034082 0.9949598
## Intervention_Redefined           0.8347624    0.981  1.153856 0.8347624
## lag_num_pd_w_tx                  0.9667396    0.985  1.004098 0.9667396
##                                   exp_coef   exp_ub          sd
## Naloxone_Pharmacy_Yes_Redefined  1.0156966 1.181570 0.077178405
## lag_num_pd_w_naloxone_yes        0.9788432 1.002062 0.011960947
## Naloxone_Pharmacy_No_Redefined   1.0639465 1.270112 0.090367361
## lag_num_pd_w_naloxone_no         0.9978635 1.014983 0.008678879
## Medical_Marijuana_Redefined      1.0341096 1.213635 0.081673107
## lag_num_pd_w_med_marijuana       0.9961851 1.011957 0.008014361
## Recreational_Marijuana_Redefined 1.0018531 1.170080 0.079194291
## lag_num_pd_w_rec_marijuana       0.9802140 1.012042 0.016303184
## GSL_Redefined                    1.0260238 1.156069 0.060884760
## lag_num_pd_w_gsl                 1.0136824 1.034205 0.010226281
## PDMP_Redefined                   0.9677572 1.146135 0.086311045
## lag_num_pd_w_pdmp                1.0046983 1.026637 0.011021019
## Medicaid_Expansion_Redefined     1.0700172 1.201518 0.059138447
## lag_num_pd_w_medicaid            1.0143325 1.034082 0.009838603
## Intervention_Redefined           0.9814253 1.153856 0.082581000
## lag_num_pd_w_tx                  0.9852418 1.004098 0.009672384
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## Intervention_Redefined                     Intervention_Redefined
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                    ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.873, 1.182)
## lag_num_pd_w_naloxone_yes        95% CI = (0.956, 1.002)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.891, 1.270)
## lag_num_pd_w_naloxone_no         95% CI = (0.981, 1.015)
## Medical_Marijuana_Redefined      95% CI = (0.881, 1.214)
## lag_num_pd_w_med_marijuana       95% CI = (0.981, 1.012)
## Recreational_Marijuana_Redefined 95% CI = (0.858, 1.170)
## lag_num_pd_w_rec_marijuana       95% CI = (0.949, 1.012)
## GSL_Redefined                    95% CI = (0.911, 1.156)
## lag_num_pd_w_gsl                 95% CI = (0.994, 1.034)
## PDMP_Redefined                   95% CI = (0.817, 1.146)
## lag_num_pd_w_pdmp                95% CI = (0.983, 1.027)
## Medicaid_Expansion_Redefined     95% CI = (0.953, 1.202)
## lag_num_pd_w_medicaid            95% CI = (0.995, 1.034)
## Intervention_Redefined           95% CI = (0.835, 1.154)
## lag_num_pd_w_tx                  95% CI = (0.967, 1.004)

5.7.2 Attributable Deaths

date_data <- sensitivity_anlys_event_study_data_lin_post_tx[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
var_cov_mat_beta_logistic_tx <- as.matrix(sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time_w_cov[[2]][
  c("Intervention_Redefined", "lag_num_pd_w_tx"), c("Intervention_Redefined", "lag_num_pd_w_tx")])

attr_deaths_est_logistic_smoothed_time_lin_post <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx,
                                                                      sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time,
                                                                      tx_name = c("Intervention_Redefined",
                                                                                                    "lag_num_pd_w_tx"),
                                                                      var_cov_mat_beta_logistic_tx)

attr_deaths_est_logistic_smoothed_time_lin_post <- merge(attr_deaths_est_logistic_smoothed_time_lin_post, date_data, 
                                                         by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_logistic_smoothed_time_lin_post_summary <- attr_deaths_est_logistic_smoothed_time_lin_post %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_attr_deaths = sum(attr_deaths),
            total_attr_deaths_lb = sum(attr_deaths_lb),
            total_attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths)
## [1] 194246
sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths_lb)
## [1] -179850.4
sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths_ub)
## [1] 568342.3
sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths)/sum(main_analysis_data$imputed_deaths)
## [1] 0.2951098
sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths_lb)/sum(main_analysis_data$imputed_deaths)
## [1] -0.2732392
sum(attr_deaths_est_logistic_smoothed_time_lin_post_summary$total_attr_deaths_ub)/sum(main_analysis_data$imputed_deaths)
## [1] 0.8634589
ggplot(attr_deaths_est_logistic_smoothed_time_lin_post_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year,
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

#overall national overdose deaths
national_od <- sensitivity_anlys_event_study_data_lin_post_tx %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_od = sum(imputed_deaths),
            total_od_prob = sum(imputed_deaths)/sum(population),
            total_pop = sum(population))



national_od <- merge(national_od, attr_deaths_est_logistic_smoothed_time_lin_post_summary, by = "year")

ggplot(national_od, aes(x = year)) + 
  geom_line(aes(y = total_od, color = "Oberved OD")) +
  geom_line(aes(y = total_attr_deaths, color = "Lives Saved", linetype = "Estimate")) + 
  geom_line(aes(y = total_attr_deaths_lb, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths_ub, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths + total_od, 
                color = "Potential Number of Deaths Had There Not Been DIH Prosecutions")) + 
  labs(x = "Date", y = "People",
       title = "Number of OD Deaths and Estimated Number of Lives Saved in Each Year",
       color = "", linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))

5.8 Analysis With Only Periods After Treatment Subset Periods

#run the gam model
data_subset <- sensitivity_anlys_event_study_data_lin_post_tx[sensitivity_anlys_event_study_data_lin_post_tx$Time_Period_ID <= 30,]
sensitivity_anlys_post_tx_model_logistic_smoothed_time_subset<-gam(cbind(round(imputed_deaths), round(num_alive))~ State +
                                                                     s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                     Naloxone_Pharmacy_Yes_Redefined + 
                                                                     lag_num_pd_w_naloxone_yes +
                                                                     Naloxone_Pharmacy_No_Redefined + 
                                                                     lag_num_pd_w_naloxone_no +
                                                                     Medical_Marijuana_Redefined + 
                                                                     lag_num_pd_w_med_marijuana +
                                                                     Recreational_Marijuana_Redefined + 
                                                                     lag_num_pd_w_rec_marijuana +
                                                                     GSL_Redefined + 
                                                                     lag_num_pd_w_gsl +
                                                                     PDMP_Redefined + 
                                                                     lag_num_pd_w_pdmp +
                                                                     Medicaid_Expansion_Redefined +
                                                                     lag_num_pd_w_medicaid +
                                                                     Intervention_Redefined +
                                                                     lag_num_pd_w_tx,
                                                                   data = data_subset,
                                                                   family = "binomial")
summary(sensitivity_anlys_post_tx_model_logistic_smoothed_time_subset)
## 
## Family: binomial 
## Link function: logit 
## 
## Formula:
## cbind(round(imputed_deaths), round(num_alive)) ~ State + s(Time_Period_ID, 
##     bs = "cr", by = as.factor(Region)) + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + Intervention_Redefined + lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)                      -9.9142839  0.0169704 -584.212  < 2e-16 ***
## StateAlaska                       0.4656840  0.0395133   11.786  < 2e-16 ***
## StateArizona                      0.3260481  0.0181969   17.918  < 2e-16 ***
## StateArkansas                    -0.3816820  0.0264243  -14.444  < 2e-16 ***
## StateCalifornia                   0.1374486  0.0253367    5.425 5.80e-08 ***
## StateColorado                     0.2421707  0.0256020    9.459  < 2e-16 ***
## StateConnecticut                  0.2023745  0.0227399    8.900  < 2e-16 ***
## StateDelaware                     0.1746091  0.0329617    5.297 1.18e-07 ***
## StateFlorida                      0.5171831  0.0179741   28.774  < 2e-16 ***
## StateGeorgia                      0.2669132  0.0206240   12.942  < 2e-16 ***
## StateHawaii                      -0.3190920  0.0385663   -8.274  < 2e-16 ***
## StateIdaho                       -0.3220520  0.0325556   -9.892  < 2e-16 ***
## StateIllinois                     0.2045205  0.0202426   10.103  < 2e-16 ***
## StateIndiana                     -0.1287957  0.0204247   -6.306 2.87e-10 ***
## StateIowa                        -0.6289116  0.0277864  -22.634  < 2e-16 ***
## StateKansas                      -0.2274021  0.0245331   -9.269  < 2e-16 ***
## StateKentucky                     0.5383433  0.0196523   27.393  < 2e-16 ***
## StateLouisiana                    0.4179674  0.0188506   22.173  < 2e-16 ***
## StateMaine                        0.1200546  0.0349900    3.431 0.000601 ***
## StateMaryland                    -1.3997811  0.0323084  -43.326  < 2e-16 ***
## StateMassachusetts               -0.0196295  0.0199764   -0.983 0.325788    
## StateMichigan                    -0.0430901  0.0201842   -2.135 0.032774 *  
## StateMinnesota                   -0.5445597  0.0251231  -21.676  < 2e-16 ***
## StateMississippi                 -0.0899853  0.0230006   -3.912 9.14e-05 ***
## StateMissouri                     0.2681887  0.0207599   12.919  < 2e-16 ***
## StateMontana                     -0.0404765  0.0382705   -1.058 0.290219    
## StateNebraska                    -0.8858219  0.0371221  -23.862  < 2e-16 ***
## StateNevada                       0.6634038  0.0263718   25.156  < 2e-16 ***
## StateNew Hampshire                0.1264438  0.0289908    4.362 1.29e-05 ***
## StateNew Jersey                   0.1877752  0.0190435    9.860  < 2e-16 ***
## StateNew Mexico                   0.8956733  0.0258327   34.672  < 2e-16 ***
## StateNew York                    -0.2818616  0.0188755  -14.933  < 2e-16 ***
## StateNorth Carolina               0.2273335  0.0168711   13.475  < 2e-16 ***
## StateNorth Dakota                -1.3175979  0.0667482  -19.740  < 2e-16 ***
## StateOhio                         0.5456305  0.0183878   29.674  < 2e-16 ***
## StateOklahoma                     0.3790127  0.0205461   18.447  < 2e-16 ***
## StateOregon                       0.0479041  0.0289839    1.653 0.098375 .  
## StatePennsylvania                 0.5443358  0.0198662   27.400  < 2e-16 ***
## StateRhode Island                 0.0880039  0.0325676    2.702 0.006888 ** 
## StateSouth Carolina               0.1159933  0.0196563    5.901 3.61e-09 ***
## StateSouth Dakota                -1.0671710  0.0567210  -18.814  < 2e-16 ***
## StateTennessee                    0.3589872  0.0172473   20.814  < 2e-16 ***
## StateTexas                        0.0417948  0.0194628    2.147 0.031760 *  
## StateUtah                        -0.0404964  0.0260012   -1.557 0.119356    
## StateVermont                     -0.1079730  0.0434154   -2.487 0.012884 *  
## StateVirginia                    -0.1214341  0.0194204   -6.253 4.03e-10 ***
## StateWashington                   0.3361612  0.0252409   13.318  < 2e-16 ***
## StateWest Virginia                0.6700384  0.0222475   30.117  < 2e-16 ***
## StateWisconsin                    0.0452589  0.0202226    2.238 0.025219 *  
## StateWyoming                      0.0198349  0.0416322    0.476 0.633768    
## Naloxone_Pharmacy_Yes_Redefined  -0.0451257  0.0135442   -3.332 0.000863 ***
## lag_num_pd_w_naloxone_yes        -0.0348725  0.0036484   -9.558  < 2e-16 ***
## Naloxone_Pharmacy_No_Redefined    0.1033996  0.0105883    9.765  < 2e-16 ***
## lag_num_pd_w_naloxone_no         -0.0142420  0.0012446  -11.443  < 2e-16 ***
## Medical_Marijuana_Redefined       0.0208996  0.0101291    2.063 0.039081 *  
## lag_num_pd_w_med_marijuana       -0.0050364  0.0010727   -4.695 2.66e-06 ***
## Recreational_Marijuana_Redefined -0.1575028  0.0385241   -4.088 4.34e-05 ***
## lag_num_pd_w_rec_marijuana       -0.0170023  0.0185896   -0.915 0.360394    
## GSL_Redefined                    -0.0262166  0.0102647   -2.554 0.010648 *  
## lag_num_pd_w_gsl                  0.0005865  0.0025692    0.228 0.819417    
## PDMP_Redefined                   -0.0457857  0.0078718   -5.816 6.01e-09 ***
## lag_num_pd_w_pdmp                 0.0074035  0.0008069    9.175  < 2e-16 ***
## Medicaid_Expansion_Redefined      0.0467635  0.0104219    4.487 7.22e-06 ***
## lag_num_pd_w_medicaid             0.0173406  0.0029639    5.851 4.90e-09 ***
## Intervention_Redefined            0.0151905  0.0070978    2.140 0.032341 *  
## lag_num_pd_w_tx                  -0.0180270  0.0006636  -27.165  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df Chi.sq p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   8.530  8.932   3285  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.641  8.960   2127  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     8.681  8.969   3411  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      8.911  8.998   1468  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.882   Deviance explained = 87.3%
## UBRE = 5.5488  Scale est. = 1         n = 1480

5.8.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_logistic_smoothed_time_subset <-
  data.frame(predict(sensitivity_anlys_post_tx_model_logistic_smoothed_time_subset, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_logistic_smoothed_time_subset <- coef(sensitivity_anlys_post_tx_model_logistic_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset_w_cov <- 
  compute_sd_and_CI_logistic(as.matrix(full_df_w_basis_functions_sensitivity_anlys_post_tx_logistic_smoothed_time_subset), 
                    (data_subset$prop_dead),
                    coefficient_values_sensitivity_anlys_post_tx_logistic_smoothed_time_subset,
                    k = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_logistic_smoothed_time_subset),
                    print_full_cov = TRUE)
# sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset

sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset <- 
  sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset_w_cov[[1]]

colnames(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset) <- c("conf.low", "estimate", "conf.high",
                                                                                 "exp_lb", "exp_coef", "exp_ub", "sd")
sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_logistic_all_yr_subset <- sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset[51:66,]
table_of_RR_lin_eff_logistic_all_yr_subset$estimate <- round(exp(table_of_RR_lin_eff_logistic_all_yr_subset$estimate), 3)
table_of_RR_lin_eff_logistic_all_yr_subset$conf.low <- exp(table_of_RR_lin_eff_logistic_all_yr_subset$conf.low)
table_of_RR_lin_eff_logistic_all_yr_subset$conf.high <- exp(table_of_RR_lin_eff_logistic_all_yr_subset$conf.high)
table_of_RR_lin_eff_logistic_all_yr_subset$ci_95 <- paste("95% CI = (", 
                                                          format(round(table_of_RR_lin_eff_logistic_all_yr_subset$conf.low, 3), 
                                                                 nsmall = 3), ", ", 
                                                          format(round(table_of_RR_lin_eff_logistic_all_yr_subset$conf.high, 3), 
                                                                 nsmall = 3), ")", sep = "")

# write.csv(table_of_RR_lin_eff_logistic_all_yr_subset, "./Data/table_of_RR_lin_eff_logistic_all_yr_subset_5_13_22.csv")
table_of_RR_lin_eff_logistic_all_yr_subset
##                                   conf.low estimate conf.high    exp_lb
## Naloxone_Pharmacy_Yes_Redefined  0.7975356    0.956 1.1456561 0.7975356
## lag_num_pd_w_naloxone_yes        0.9191672    0.966 1.0146484 0.9191672
## Naloxone_Pharmacy_No_Redefined   0.8671666    1.109 1.4181075 0.8671666
## lag_num_pd_w_naloxone_no         0.9707764    0.986 1.0011758 0.9707764
## Medical_Marijuana_Redefined      0.8404603    1.021 1.2406119 0.8404603
## lag_num_pd_w_med_marijuana       0.9782338    0.995 1.0120053 0.9782338
## Recreational_Marijuana_Redefined 0.6278541    0.854 1.1623476 0.6278541
## lag_num_pd_w_rec_marijuana       0.8793360    0.983 1.0992011 0.8793360
## GSL_Redefined                    0.8431123    0.974 1.1254939 0.8431123
## lag_num_pd_w_gsl                 0.9761370    1.001 1.0256488 0.9761370
## PDMP_Redefined                   0.8120778    0.955 1.1236560 0.8120778
## lag_num_pd_w_pdmp                0.9860868    1.007 1.0292370 0.9860868
## Medicaid_Expansion_Redefined     0.9032576    1.048 1.2156447 0.9032576
## lag_num_pd_w_medicaid            0.9743360    1.017 1.0625593 0.9743360
## Intervention_Redefined           0.8799492    1.015 1.1714849 0.8799492
## lag_num_pd_w_tx                  0.9677675    0.982 0.9967149 0.9677675
##                                   exp_coef    exp_ub          sd
## Naloxone_Pharmacy_Yes_Redefined  0.9558774 1.1456561 0.092399585
## lag_num_pd_w_naloxone_yes        0.9657285 1.0146484 0.025211573
## Naloxone_Pharmacy_No_Redefined   1.1089344 1.4181075 0.125471269
## lag_num_pd_w_naloxone_no         0.9858590 1.0011758 0.007865877
## Medical_Marijuana_Redefined      1.0211195 1.2406119 0.099339364
## lag_num_pd_w_med_marijuana       0.9949763 1.0120053 0.008658247
## Recreational_Marijuana_Redefined 0.8542744 1.1623476 0.157114571
## lag_num_pd_w_rec_marijuana       0.9831414 1.0992011 0.056931590
## GSL_Redefined                    0.9741241 1.1254939 0.073693121
## lag_num_pd_w_gsl                 1.0005867 1.0256488 0.012621863
## PDMP_Redefined                   0.9552466 1.1236560 0.082843573
## lag_num_pd_w_pdmp                1.0074309 1.0292370 0.010925678
## Medicaid_Expansion_Redefined     1.0478742 1.2156447 0.075770939
## lag_num_pd_w_medicaid            1.0174919 1.0625593 0.022112113
## Intervention_Redefined           1.0153064 1.1714849 0.073000832
## lag_num_pd_w_tx                  0.9821346 0.9967149 0.007518587
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## Intervention_Redefined                     Intervention_Redefined
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                    ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.798, 1.146)
## lag_num_pd_w_naloxone_yes        95% CI = (0.919, 1.015)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.867, 1.418)
## lag_num_pd_w_naloxone_no         95% CI = (0.971, 1.001)
## Medical_Marijuana_Redefined      95% CI = (0.840, 1.241)
## lag_num_pd_w_med_marijuana       95% CI = (0.978, 1.012)
## Recreational_Marijuana_Redefined 95% CI = (0.628, 1.162)
## lag_num_pd_w_rec_marijuana       95% CI = (0.879, 1.099)
## GSL_Redefined                    95% CI = (0.843, 1.125)
## lag_num_pd_w_gsl                 95% CI = (0.976, 1.026)
## PDMP_Redefined                   95% CI = (0.812, 1.124)
## lag_num_pd_w_pdmp                95% CI = (0.986, 1.029)
## Medicaid_Expansion_Redefined     95% CI = (0.903, 1.216)
## lag_num_pd_w_medicaid            95% CI = (0.974, 1.063)
## Intervention_Redefined           95% CI = (0.880, 1.171)
## lag_num_pd_w_tx                  95% CI = (0.968, 0.997)

5.8.2 Attributable Deaths

date_data_subset <- data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset <- date_data_subset[!duplicated(date_data_subset),]
var_cov_mat_beta_logistic_tx_subset <- as.matrix(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset_w_cov[[2]][
  c("Intervention_Redefined", "lag_num_pd_w_tx"), c("Intervention_Redefined", "lag_num_pd_w_tx")])
attr_deaths_est_logistic_smoothed_time_lin_post_subset <- attr_death_compute(
  data_subset,
  sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset, 
  tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"),
  var_cov_mat_beta_logistic_tx_subset)
attr_deaths_est_logistic_smoothed_time_lin_post_subset <- merge(attr_deaths_est_logistic_smoothed_time_lin_post_subset, date_data_subset, 
                                                                by.x = "Time_Period", by.y = "Time_Period_ID")


attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary <- attr_deaths_est_logistic_smoothed_time_lin_post_subset %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_attr_deaths = sum(attr_deaths),
            total_attr_deaths_lb = sum(attr_deaths_lb),
            total_attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths)
## [1] 65061.87
sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths_lb)
## [1] -32979.83
sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths_ub)
## [1] 163103.6
sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths)/sum(data_subset$imputed_deaths)
## [1] 0.1716333
sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths_lb)/sum(data_subset$imputed_deaths)
## [1] -0.08700085
sum(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary$total_attr_deaths_ub)/sum(data_subset$imputed_deaths)
## [1] 0.4302675
ggplot(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year 
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

ggplot() + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths, linetype = "Estimate",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_lb, linetype = "95% CI",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_ub, linetype = "95% CI",
                          color = "Full Dataset")) + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths, linetype = "Estimate",
                          color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths_lb, 
                          linetype = "95% CI",
                          color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_attr_deaths_ub, 
                          linetype = "95% CI",
                          color = "Subset Dataset")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "", color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6 OLS Model Two Year With Linear Fixed Time Effects Interacted with Region With Log Proportion

6.1 Create Two Year Data

#create a plot for each state to see how many prosecution media alerts there are per 6 month period
#read in the prosecution media alert data
prosecution_data<-read.csv("./Data/dih_prosecutions_9_6_21.csv")

#data cleaning
prosecution_data<-prosecution_data %>% 
  mutate(Date = as.Date(Date.charged, "%m/%d/%Y")) %>%
  mutate(State = ifelse(State.Filed == "pennsylvania", "Pennsylvania", State.Filed),
         State = ifelse(State.Filed == "Virginia ", "Virginia", State)) %>%
  filter(!is.na(Date), State.Filed != "No Info", State.Filed != "No info", State.Filed != "No Info ",
         State != "")

#clean up the data by looking at the link to the article
prosecution_data$Date[prosecution_data$Date == "2026-08-01"] <- as.Date("2016-02-15", "%Y-%m-%d")

#change the states into Character instead of factor
prosecution_data$State<-as.character(prosecution_data$State)
#see how many prosecution data points there are for each state
table(prosecution_data$State)
## 
##        Alabama         Alaska        Arizona       Arkansas     California 
##             12              8              9              4             76 
##       Colorado    Connecticut       Delaware        Florida        Georgia 
##             32             47              3            138             29 
##          Idaho       Illinois        Indiana           Iowa         Kansas 
##              9            342             55             31              9 
##       Kentucky      Louisiana          Maine       Maryland  Massachusetts 
##             43             65             17             63             34 
##       Michigan      Minnesota    Mississippi       Missouri        Montana 
##            116            140              1             45             11 
##       Nebraska         Nevada  New Hampshire     New Jersey     New Mexico 
##              1             13             42            137              4 
##       New York North Carolina   North Dakota           Ohio       Oklahoma 
##            110            124             53            404             41 
##         Oregon   Pennsylvania   Rhode Island South Carolina   South Dakota 
##             19            726              2             12             13 
##      Tennessee          Texas           Utah        Vermont       Virginia 
##             94             44             21             13             63 
##     Washington  West Virginia      Wisconsin        Wyoming 
##             78             33            381             19
#there are some repeated cases depending on victim
prosecution_data_unique <- prosecution_data %>%
  group_by(State) %>%
  distinct(Accused.Name, Date, .keep_all = T)
table(prosecution_data_unique$State)
## 
##        Alabama         Alaska        Arizona       Arkansas     California 
##             12              8              9              4             72 
##       Colorado    Connecticut       Delaware        Florida        Georgia 
##             30             46              3            134             26 
##          Idaho       Illinois        Indiana           Iowa         Kansas 
##              9            336             53             31              9 
##       Kentucky      Louisiana          Maine       Maryland  Massachusetts 
##             43             65             17             62             34 
##       Michigan      Minnesota    Mississippi       Missouri        Montana 
##            114            140              1             44             10 
##       Nebraska         Nevada  New Hampshire     New Jersey     New Mexico 
##              1             13             42            131              4 
##       New York North Carolina   North Dakota           Ohio       Oklahoma 
##            105            121             40            395             34 
##         Oregon   Pennsylvania   Rhode Island South Carolina   South Dakota 
##             19            718              2             12             13 
##      Tennessee          Texas           Utah        Vermont       Virginia 
##             94             43             21             13             63 
##     Washington  West Virginia      Wisconsin        Wyoming 
##             75             33            373             19
#change date charged into Date object
prosecution_data_unique$Date<-mdy(prosecution_data_unique$Date.charged)

#group the data into six month periods
prosecution_data_unique<-prosecution_data_unique %>% 
  mutate(six_month_pd = lubridate::floor_date(Date , "6 months" ))

#count the number of prosecution media alerts in each six month period
#we also get the first and last date of prosecution in time period
prosecution_data_by_six_month_pd <- prosecution_data_unique %>%
  filter(year(six_month_pd)>1999 & year(six_month_pd)<2020) %>%
  group_by(State, six_month_pd) %>%
  summarise(first_date_in_pd = min(Date), last_date_in_pd = max(Date))

#create the data set used for this sensitivity analysis
#first, we merge the grouped prosecution data set with the main data set by state and time period
sensitivity_anlys_redefine_int_data<-merge(main_analysis_data,
                                           prosecution_data_by_six_month_pd,
                                           by.x = c("State", "Time_Period_Start"),
                                           by.y = c("State", "six_month_pd"), all = TRUE)

#create a intervention 2 year effect variable by initializing it to be all 0
sensitivity_anlys_redefine_int_data<-sensitivity_anlys_redefine_int_data %>%
  group_by(State) %>%
  mutate(int_2_yr_effect = 0)

#change the date into a date object
sensitivity_anlys_redefine_int_data$Time_Period_Start<-as.Date(sensitivity_anlys_redefine_int_data$Time_Period_Start)
sensitivity_anlys_redefine_int_data$Time_Period_End<-as.Date(sensitivity_anlys_redefine_int_data$Time_Period_End)

#we need to impute the newly defined intervention variable depending on the case
#by examining each row of the data set
for(state in unique(sensitivity_anlys_redefine_int_data$State)){
  #first, subset the data set into state_data which only contains the data for the state
  state_index<-which(sensitivity_anlys_redefine_int_data$State == state)
  state_data<-sensitivity_anlys_redefine_int_data[state_index,]
  
  #note that the first four rows of the 2 year effect intervention variable are the same as the
  #first four rows of the original intervention variable
  state_data$int_2_yr_effect[1:4]<-state_data$Intervention_Redefined[1:4]
  
  for(i in 5:nrow(state_data)){
    #next, we deal with the rows where there was at least one prosecution in the last 3 six month periods
    #These rows will be imputed with a 1
    if((!is.na(state_data$first_date_in_pd[i - 1]) |
        !is.na(state_data$first_date_in_pd[i - 2]) |
        !is.na(state_data$first_date_in_pd[i - 3]))){
      
      state_data$int_2_yr_effect[i]<-1
      
    }else{
      #next, we account for the rows with the fractions:
      # 1) an intervention occurs in row i without an intervention 2 years ago
      # 2) row i contains the lasting effects of an intervention that occurred 2 years ago
      # 3) row i contains effects from both a new intervention starting in row i and lasting
      # effects from 2 years ago
      
      #To compute the fraction, we add the number of days that are affected by an intervention
      #(from both the current prosecution and previous prosecution) and then divide by the total
      #number of days in the period:
      
      total_len_of_pd<-as.numeric(state_data$Time_Period_End[i] - state_data$Time_Period_Start[i])
      
      #If there is no prosecution two years ago, i.e. in period i-4, then the last_date is the first
      #date in period i. We subtract the last_date by the first date in the period, so we will get
      #a 0 for the number of days that are affected by a prosecution from period i-4. Otherwise,
      #the last_date is the last date of prosecution from period i-4, plus 2 years.
      len_of_past_effect <- ifelse(!is.na(state_data$first_date_in_pd[i - 4]),
                                   (state_data$last_date_in_pd[i - 4] + years(2)) - state_data$Time_Period_Start[i],
                                   0)
      
      #If there is no prosecution in the period i, then the start_date is the last date in the period i.
      #We subtract start_date from the last date in period i, so we will get a 0 for the number
      #of days that are affected by a prosecution in period i. Otherwise, the start_date is the
      #first date of a prosecution in period i.
      len_of_current_effect <- ifelse(!is.na(state_data$first_date_in_pd[i]),
                                      as.numeric(state_data$Time_Period_End[i] - state_data$first_date_in_pd[i]),
                                      0)
      
      state_data$int_2_yr_effect[i]<-(len_of_past_effect + len_of_current_effect)/total_len_of_pd
    }
  }
  
  #for the case where the int_2_yr_effect is greater than 1 (could result when we add the effects of
  #previous intervention and the current intervention), we just impute a 1 instead
  state_data$int_2_yr_effect[state_data$int_2_yr_effect>1]<-1
  
  #lastly, we store the int_2_yr_effect variable into the sensitivity analysis data set
  sensitivity_anlys_redefine_int_data$int_2_yr_effect[state_index]<-state_data$int_2_yr_effect
}

#view the data set just to make sure the imputation looks right
# View(sensitivity_anlys_redefine_int_data %>% select(State, Time_Period_Start, Time_Period_End,
#                                                     Intervention_Redefined, first_date_in_pd,
#                                                     last_date_in_pd,
#                                                     int_2_yr_effect))


sensitivity_anlys_redefine_int_data <- sensitivity_anlys_redefine_int_data %>%
  group_by(Time_Period_Start) %>%
  mutate(num_states_w_intervention_2_yr_effect = sum(int_2_yr_effect))

6.2 Constant Effect Model

#compute the proportion of people who died from drug overdose
sensitivity_anlys_redefine_int_data$prop_dead <- sensitivity_anlys_redefine_int_data$imputed_deaths/
  sensitivity_anlys_redefine_int_data$population


#fit an OLS with smoothed time effects
sensitivity_analysis_model_log_fixed_lin_time<-gam(log(prop_dead)~ State +
                                                     s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
                                                     Naloxone_Pharmacy_Yes_Redefined +
                                                     Naloxone_Pharmacy_No_Redefined +
                                                     Medical_Marijuana_Redefined +
                                                     Recreational_Marijuana_Redefined +
                                                     GSL_Redefined +
                                                     PDMP_Redefined +
                                                     Medicaid_Expansion_Redefined +
                                                     int_2_yr_effect+
                                                     num_states_w_intervention,
                                                   data = sensitivity_anlys_redefine_int_data)

summary(sensitivity_analysis_model_log_fixed_lin_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + Naloxone_Pharmacy_No_Redefined + 
##     Medical_Marijuana_Redefined + Recreational_Marijuana_Redefined + 
##     GSL_Redefined + PDMP_Redefined + Medicaid_Expansion_Redefined + 
##     int_2_yr_effect + num_states_w_intervention
## 
## Parametric coefficients:
##                                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      -9.587226   0.206611 -46.402  < 2e-16 ***
## StateAlaska                       0.176755   0.074110   2.385 0.017173 *  
## StateArizona                      0.297814   0.067760   4.395 1.17e-05 ***
## StateArkansas                    -0.476857   0.067149  -7.101 1.73e-12 ***
## StateCalifornia                  -0.166194   0.074100  -2.243 0.025022 *  
## StateColorado                     0.041539   0.074077   0.561 0.575032    
## StateConnecticut                  0.229306   0.070949   3.232 0.001250 ** 
## StateDelaware                     0.231812   0.067637   3.427 0.000622 ***
## StateFlorida                      0.292570   0.066890   4.374 1.29e-05 ***
## StateGeorgia                     -0.011507   0.066066  -0.174 0.861748    
## StateHawaii                      -0.357499   0.072364  -4.940 8.48e-07 ***
## StateIdaho                       -0.109817   0.066494  -1.652 0.098794 .  
## StateIllinois                     0.149789   0.067855   2.207 0.027398 *  
## StateIndiana                      0.085336   0.066318   1.287 0.198332    
## StateIowa                        -0.682290   0.066546 -10.253  < 2e-16 ***
## StateKansas                      -0.228069   0.066237  -3.443 0.000587 ***
## StateKentucky                     0.709540   0.066659  10.644  < 2e-16 ***
## StateLouisiana                    0.332745   0.065957   5.045 4.97e-07 ***
## StateMaine                        0.089500   0.073896   1.211 0.225978    
## StateMaryland                    -1.524850   0.067636 -22.545  < 2e-16 ***
## StateMassachusetts               -0.082565   0.067368  -1.226 0.220511    
## StateMichigan                     0.004112   0.068491   0.060 0.952135    
## StateMinnesota                   -0.645647   0.069966  -9.228  < 2e-16 ***
## StateMississippi                 -0.035310   0.066191  -0.533 0.593774    
## StateMissouri                     0.161697   0.068250   2.369 0.017926 *  
## StateMontana                     -0.447379   0.070077  -6.384 2.16e-10 ***
## StateNebraska                    -0.862496   0.067412 -12.794  < 2e-16 ***
## StateNevada                       0.456589   0.071704   6.368 2.39e-10 ***
## StateNew Hampshire                0.150233   0.067124   2.238 0.025326 *  
## StateNew Jersey                   0.071547   0.067679   1.057 0.290574    
## StateNew Mexico                   0.651696   0.073147   8.909  < 2e-16 ***
## StateNew York                    -0.140480   0.068398  -2.054 0.040125 *  
## StateNorth Carolina               0.227585   0.065860   3.456 0.000561 ***
## StateNorth Dakota                -1.128784   0.066280 -17.031  < 2e-16 ***
## StateOhio                         0.436852   0.067016   6.519 9.04e-11 ***
## StateOklahoma                     0.470085   0.066382   7.082 1.99e-12 ***
## StateOregon                      -0.269708   0.073847  -3.652 0.000267 ***
## StatePennsylvania                 0.546165   0.066981   8.154 6.28e-16 ***
## StateRhode Island                -0.266634   0.069456  -3.839 0.000128 ***
## StateSouth Carolina               0.222520   0.066060   3.368 0.000771 ***
## StateSouth Dakota                -1.011055   0.066392 -15.229  < 2e-16 ***
## StateTennessee                    0.470795   0.065773   7.158 1.16e-12 ***
## StateTexas                       -0.029711   0.066420  -0.447 0.654699    
## StateUtah                        -0.098865   0.066000  -1.498 0.134308    
## StateVermont                     -0.165944   0.069785  -2.378 0.017507 *  
## StateVirginia                    -0.041036   0.065986  -0.622 0.534082    
## StateWashington                   0.044168   0.075288   0.587 0.557503    
## StateWest Virginia                0.799829   0.066610  12.008  < 2e-16 ***
## StateWisconsin                   -0.002925   0.066243  -0.044 0.964779    
## StateWyoming                     -0.017694   0.066098  -0.268 0.788959    
## Naloxone_Pharmacy_Yes_Redefined  -0.084451   0.042697  -1.978 0.048080 *  
## Naloxone_Pharmacy_No_Redefined   -0.005666   0.038650  -0.147 0.883470    
## Medical_Marijuana_Redefined       0.194513   0.030661   6.344 2.79e-10 ***
## Recreational_Marijuana_Redefined -0.115195   0.048870  -2.357 0.018514 *  
## GSL_Redefined                     0.057399   0.031574   1.818 0.069236 .  
## PDMP_Redefined                   -0.153999   0.024689  -6.237 5.46e-10 ***
## Medicaid_Expansion_Redefined      0.089477   0.030098   2.973 0.002987 ** 
## int_2_yr_effect                   0.013794   0.021560   0.640 0.522385    
## num_states_w_intervention        -0.005061   0.006998  -0.723 0.469647    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.681  5.753 12.19  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.457  8.915 17.85  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.371  7.516 12.46  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      3.057  3.882 10.79  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.84   Deviance explained = 84.6%
## GCV = 0.089684  Scale est. = 0.086026  n = 2000
#examine fitted values
summary(fitted(sensitivity_analysis_model_log_fixed_lin_time))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -12.329 -10.208  -9.738  -9.796  -9.344  -8.133
hist(fitted(sensitivity_analysis_model_log_fixed_lin_time))

par(mfrow = c(2,2))
plot(sensitivity_analysis_model_log_fixed_lin_time)

6.2.1 Coefficients and 95% CI

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_2_yr <- model.matrix(sensitivity_analysis_model_log_fixed_lin_time)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_sens_anlys <- coef(sensitivity_analysis_model_log_fixed_lin_time)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time <- compute_sd_and_CI_linear_link(full_df_w_basis_functions_log_fixed_lin_time_2_yr,
                                                                log(sensitivity_anlys_redefine_int_data$prop_dead),
                                                                coefficient_values_log_fixed_lin_time_sens_anlys,
                                                                k = ncol(full_df_w_basis_functions_log_fixed_lin_time_2_yr))

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time$term <- rownames(sens_analysis_sd_and_ci_log_fixed_lin_time)


sens_analysis_sd_and_ci_log_fixed_lin_time$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time[51:59,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Effects, 
       2 Year Exposure Intervention") + 
  scale_color_grey()     + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time[51:59,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 9:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time[51:59,], 
            mapping = aes(label = ci_95, x = 0.9, y = 9:1), size = 3) +
  xlim(-.3, 1.1)

two_yr_analysis_coef_of_interest_RR <- sens_analysis_sd_and_ci_log_fixed_lin_time[51:59,]
two_yr_analysis_coef_of_interest_RR$est_RR <- round(exp(two_yr_analysis_coef_of_interest_RR$estimate), 3)
two_yr_analysis_coef_of_interest_RR$est_RR_lb <- round(exp(two_yr_analysis_coef_of_interest_RR$conf.low), 3)
two_yr_analysis_coef_of_interest_RR$est_RR_ub <- round(exp(two_yr_analysis_coef_of_interest_RR$conf.high), 3)

two_yr_analysis_coef_of_interest_RR[,c("est_RR", "est_RR_lb", "est_RR_ub")]
##                                  est_RR est_RR_lb est_RR_ub
## Naloxone_Pharmacy_Yes_Redefined   0.919     0.860     0.983
## Naloxone_Pharmacy_No_Redefined    0.994     0.932     1.061
## Medical_Marijuana_Redefined       1.215     1.134     1.301
## Recreational_Marijuana_Redefined  0.891     0.829     0.958
## GSL_Redefined                     1.059     1.003     1.119
## PDMP_Redefined                    0.857     0.812     0.905
## Medicaid_Expansion_Redefined      1.094     1.038     1.152
## int_2_yr_effect                   1.014     0.974     1.056
## num_states_w_intervention         0.995     0.978     1.012

6.3 Create Linear Trend Data

#use this function to compute the cumulative sum, but resets the sum if the variable was 0
compute_cumsum = function(x){
  cs = cumsum(x)
  cs - cummax((x == 0) * cs)
}
sensitivity_anlys_event_study_data_lin_post_tx_2yr <- sensitivity_anlys_redefine_int_data %>%
  arrange(State, Time_Period_ID) %>%
  group_by(State) %>%
  mutate(num_pd_w_tx = compute_cumsum(int_2_yr_effect),
         num_pd_w_naloxone_yes = compute_cumsum(Naloxone_Pharmacy_Yes_Redefined),
         num_pd_w_naloxone_no = compute_cumsum(Naloxone_Pharmacy_No_Redefined),
         num_pd_w_med_marijuana = compute_cumsum(Medical_Marijuana_Redefined),
         num_pd_w_rec_marijuana = compute_cumsum(Recreational_Marijuana_Redefined),
         num_pd_w_gsl = compute_cumsum(GSL_Redefined),
         num_pd_w_pdmp = compute_cumsum(PDMP_Redefined),
         num_pd_w_medicaid = compute_cumsum(Medicaid_Expansion_Redefined),
         lag_num_pd_w_tx = lag(num_pd_w_tx),
         lag_num_pd_w_naloxone_yes = lag(num_pd_w_naloxone_yes),
         lag_num_pd_w_naloxone_no = lag(num_pd_w_naloxone_no),
         lag_num_pd_w_med_marijuana = lag(num_pd_w_med_marijuana),
         lag_num_pd_w_rec_marijuana = lag(num_pd_w_rec_marijuana),
         lag_num_pd_w_gsl = lag(num_pd_w_gsl),
         lag_num_pd_w_pdmp = lag(num_pd_w_pdmp),
         lag_num_pd_w_medicaid = lag(num_pd_w_medicaid)) #lag so that intercept = effect when tx first occurs

#fill in a 0 for the NAs so we keep all the data and at most this will be 0
sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_tx[
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$Intervention_Redefined < 1] <- 
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_yes[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$Naloxone_Pharmacy_Yes_Redefined < 1]<-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_no[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$Naloxone_Pharmacy_No_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_med_marijuana[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$Medical_Marijuana_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_rec_marijuana[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$Recreational_Marijuana_Redefined < 1] <- 
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_gsl[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$GSL_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_pdmp[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$PDMP_Redefined < 1] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_medicaid[
    sensitivity_anlys_event_study_data_lin_post_tx_2yr$Medicaid_Expansion_Redefined < 1] <-0

#for the linear effects, we want the 2 year effect variable to be 0 when treatment is 0
sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_tx[
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$int_2_yr_effect == 0] <- 0

6.4 Model

#run the gam model
sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr<-gam(log(prop_dead)~ State +
                                                                 s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                 Naloxone_Pharmacy_Yes_Redefined + 
                                                                 lag_num_pd_w_naloxone_yes +
                                                                 Naloxone_Pharmacy_No_Redefined + 
                                                                 lag_num_pd_w_naloxone_no +
                                                                 Medical_Marijuana_Redefined + 
                                                                 lag_num_pd_w_med_marijuana +
                                                                 Recreational_Marijuana_Redefined + 
                                                                 lag_num_pd_w_rec_marijuana +
                                                                 GSL_Redefined + 
                                                                 lag_num_pd_w_gsl +
                                                                 PDMP_Redefined + 
                                                                 lag_num_pd_w_pdmp +
                                                                 Medicaid_Expansion_Redefined +
                                                                 lag_num_pd_w_medicaid +
                                                                 int_2_yr_effect +
                                                                 lag_num_pd_w_tx,
                                                               data = sensitivity_anlys_event_study_data_lin_post_tx_2yr)
summary(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + int_2_yr_effect + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.8016408  0.0603781 -162.338  < 2e-16 ***
## StateAlaska                       0.2565205  0.0831882    3.084 0.002075 ** 
## StateArizona                      0.3146632  0.0661622    4.756 2.12e-06 ***
## StateArkansas                    -0.4549501  0.0676579   -6.724 2.33e-11 ***
## StateCalifornia                  -0.1176174  0.0839782   -1.401 0.161507    
## StateColorado                     0.1146721  0.0788744    1.454 0.146152    
## StateConnecticut                  0.3063227  0.0726409    4.217 2.59e-05 ***
## StateDelaware                     0.2721045  0.0674638    4.033 5.72e-05 ***
## StateFlorida                      0.4583158  0.0708122    6.472 1.23e-10 ***
## StateGeorgia                      0.0791306  0.0667120    1.186 0.235711    
## StateHawaii                      -0.3283111  0.0852120   -3.853 0.000121 ***
## StateIdaho                       -0.1840170  0.0691457   -2.661 0.007850 ** 
## StateIllinois                     0.2602359  0.0750998    3.465 0.000542 ***
## StateIndiana                      0.0244260  0.0688179    0.355 0.722676    
## StateIowa                        -0.6577224  0.0644710  -10.202  < 2e-16 ***
## StateKansas                      -0.2122227  0.0646445   -3.283 0.001046 ** 
## StateKentucky                     0.6504241  0.0690747    9.416  < 2e-16 ***
## StateLouisiana                    0.3682382  0.0636885    5.782 8.62e-09 ***
## StateMaine                        0.2210305  0.0836490    2.642 0.008301 ** 
## StateMaryland                    -1.4972262  0.0692611  -21.617  < 2e-16 ***
## StateMassachusetts               -0.1690128  0.0701129   -2.411 0.016022 *  
## StateMichigan                     0.0100106  0.0734036    0.136 0.891537    
## StateMinnesota                   -0.5358930  0.0712043   -7.526 8.04e-14 ***
## StateMississippi                 -0.0552243  0.0635471   -0.869 0.384941    
## StateMissouri                     0.2880831  0.0738737    3.900 9.97e-05 ***
## StateMontana                     -0.3022387  0.0740810   -4.080 4.69e-05 ***
## StateNebraska                    -0.8363314  0.0676704  -12.359  < 2e-16 ***
## StateNevada                       0.4478611  0.0812396    5.513 4.02e-08 ***
## StateNew Hampshire                0.2365112  0.0683898    3.458 0.000556 ***
## StateNew Jersey                   0.1125847  0.0663249    1.697 0.089772 .  
## StateNew Mexico                   0.6914790  0.0793237    8.717  < 2e-16 ***
## StateNew York                    -0.2494378  0.0711911   -3.504 0.000469 ***
## StateNorth Carolina               0.2634269  0.0635776    4.143 3.57e-05 ***
## StateNorth Dakota                -1.1415178  0.0637344  -17.911  < 2e-16 ***
## StateOhio                         0.5811796  0.0695838    8.352  < 2e-16 ***
## StateOklahoma                     0.4220487  0.0690067    6.116 1.16e-09 ***
## StateOregon                      -0.0467051  0.0822708   -0.568 0.570306    
## StatePennsylvania                 0.6086714  0.0738247    8.245 3.06e-16 ***
## StateRhode Island                -0.2622975  0.0747520   -3.509 0.000460 ***
## StateSouth Carolina               0.2182717  0.0635182    3.436 0.000602 ***
## StateSouth Dakota                -0.9755882  0.0659198  -14.800  < 2e-16 ***
## StateTennessee                    0.4438194  0.0639161    6.944 5.23e-12 ***
## StateTexas                       -0.0603077  0.0692513   -0.871 0.383945    
## StateUtah                        -0.1783509  0.0689030   -2.588 0.009715 ** 
## StateVermont                     -0.0869026  0.0705338   -1.232 0.218076    
## StateVirginia                     0.0106720  0.0666013    0.160 0.872712    
## StateWashington                   0.1415374  0.0810681    1.746 0.080990 .  
## StateWest Virginia                0.7132549  0.0693991   10.278  < 2e-16 ***
## StateWisconsin                    0.1312001  0.0680598    1.928 0.054040 .  
## StateWyoming                     -0.0559858  0.0641792   -0.872 0.383136    
## Naloxone_Pharmacy_Yes_Redefined  -0.0556536  0.0421378   -1.321 0.186744    
## lag_num_pd_w_naloxone_yes        -0.0285747  0.0066092   -4.323 1.62e-05 ***
## Naloxone_Pharmacy_No_Redefined    0.1220399  0.0434310    2.810 0.005006 ** 
## lag_num_pd_w_naloxone_no         -0.0221860  0.0045497   -4.876 1.17e-06 ***
## Medical_Marijuana_Redefined       0.1991293  0.0304565    6.538 7.99e-11 ***
## lag_num_pd_w_med_marijuana       -0.0083811  0.0022239   -3.769 0.000169 ***
## Recreational_Marijuana_Redefined -0.0349421  0.0614315   -0.569 0.569561    
## lag_num_pd_w_rec_marijuana       -0.0002178  0.0109761   -0.020 0.984173    
## GSL_Redefined                     0.0847980  0.0329650    2.572 0.010176 *  
## lag_num_pd_w_gsl                  0.0102671  0.0040724    2.521 0.011780 *  
## PDMP_Redefined                   -0.1227586  0.0271925   -4.514 6.74e-06 ***
## lag_num_pd_w_pdmp                 0.0072700  0.0024211    3.003 0.002711 ** 
## Medicaid_Expansion_Redefined      0.0795666  0.0335742    2.370 0.017894 *  
## lag_num_pd_w_medicaid             0.0153612  0.0050474    3.043 0.002372 ** 
## int_2_yr_effect                   0.0465435  0.0208954    2.227 0.026034 *  
## lag_num_pd_w_tx                  -0.0116242  0.0018350   -6.335 2.96e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.124  5.102 90.93  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.422  8.905 43.37  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.397  7.545 48.20  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      4.447  5.494 30.11  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.85   Deviance explained = 85.7%
## GCV = 0.082683  Scale est. = 0.07895   n = 1980
plot(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr, pages = 1)

6.4.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr <- model.matrix(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx_2yr <- coef(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_w_cov <-
  compute_sd_and_CI_linear_link(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr,
                    log(sensitivity_anlys_event_study_data_lin_post_tx_2yr$prop_dead),
                    coefficient_values_log_fixed_lin_time_lin_post_tx_2yr,
                    k = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr),
                    print_full_cov = TRUE)
# format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx,4)

sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx <- sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_w_cov[[1]]

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term <- rownames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx)




sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention") + 
  scale_color_grey()      + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.3, 1.1)

table_of_RR_lin_eff_log_2_yr <- sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,]
table_of_RR_lin_eff_log_2_yr$estimate <- round(exp(table_of_RR_lin_eff_log_2_yr$estimate), 4)
table_of_RR_lin_eff_log_2_yr$conf.low <- exp(table_of_RR_lin_eff_log_2_yr$conf.low)
table_of_RR_lin_eff_log_2_yr$conf.high <- exp(table_of_RR_lin_eff_log_2_yr$conf.high)
table_of_RR_lin_eff_log_2_yr$ci_95 <- paste("95% CI = (", 
                                            format(round(table_of_RR_lin_eff_log_2_yr$conf.low, 4), nsmall = 4), ", ", 
                                            format(round(table_of_RR_lin_eff_log_2_yr$conf.high, 4), nsmall = 4), ")", sep = "")

# write.csv(table_of_RR_lin_eff_log_2_yr, "./Data/table_of_RR_lin_eff_log_2_yr_5_13_22.csv")
table_of_RR_lin_eff_log_2_yr
##                                   conf.low estimate conf.high          sd
## Naloxone_Pharmacy_Yes_Redefined  0.8046184   0.9459 1.1119109 0.082517147
## lag_num_pd_w_naloxone_yes        0.9449871   0.9718 0.9994347 0.014290457
## Naloxone_Pharmacy_No_Redefined   0.8805987   1.1298 1.4495207 0.127139342
## lag_num_pd_w_naloxone_no         0.9582591   0.9781 0.9982667 0.010434265
## Medical_Marijuana_Redefined      0.9897820   1.2203 1.5046031 0.106836670
## lag_num_pd_w_med_marijuana       0.9768132   0.9917 1.0067201 0.007693217
## Recreational_Marijuana_Redefined 0.7583351   0.9657 1.2296700 0.123310130
## lag_num_pd_w_rec_marijuana       0.9686836   0.9998 1.0318793 0.016122198
## GSL_Redefined                    0.9485579   1.0885 1.2490815 0.070209410
## lag_num_pd_w_gsl                 0.9903124   1.0103 1.0307318 0.010205082
## PDMP_Redefined                   0.7454302   0.8845 1.0494609 0.087262875
## lag_num_pd_w_pdmp                0.9880633   1.0073 1.0269041 0.009835979
## Medicaid_Expansion_Redefined     0.9324227   1.0828 1.2574706 0.076293708
## lag_num_pd_w_medicaid            0.9928387   1.0155 1.0386372 0.011504208
## int_2_yr_effect                  0.9262592   1.0476 1.1849353 0.062828926
## lag_num_pd_w_tx                  0.9781190   0.9884 0.9988762 0.005356997
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## int_2_yr_effect                                   int_2_yr_effect
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                      ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.8046, 1.1119)
## lag_num_pd_w_naloxone_yes        95% CI = (0.9450, 0.9994)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.8806, 1.4495)
## lag_num_pd_w_naloxone_no         95% CI = (0.9583, 0.9983)
## Medical_Marijuana_Redefined      95% CI = (0.9898, 1.5046)
## lag_num_pd_w_med_marijuana       95% CI = (0.9768, 1.0067)
## Recreational_Marijuana_Redefined 95% CI = (0.7583, 1.2297)
## lag_num_pd_w_rec_marijuana       95% CI = (0.9687, 1.0319)
## GSL_Redefined                    95% CI = (0.9486, 1.2491)
## lag_num_pd_w_gsl                 95% CI = (0.9903, 1.0307)
## PDMP_Redefined                   95% CI = (0.7454, 1.0495)
## lag_num_pd_w_pdmp                95% CI = (0.9881, 1.0269)
## Medicaid_Expansion_Redefined     95% CI = (0.9324, 1.2575)
## lag_num_pd_w_medicaid            95% CI = (0.9928, 1.0386)
## int_2_yr_effect                  95% CI = (0.9263, 1.1849)
## lag_num_pd_w_tx                  95% CI = (0.9781, 0.9989)

6.4.2 Plot Effects

plot_data <- data.frame(time_after_tx = 0:38,
                        coef = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                            sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.low = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                            sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.high = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                            sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38))

ggplot(plot_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

6.4.3 Attributable Deaths

date_data_2yr <- sensitivity_anlys_event_study_data_lin_post_tx_2yr[, c("Time_Period_ID", "Time_Period_Start")]
date_data_2yr <- date_data_2yr[!duplicated(date_data_2yr),]
var_cov_mat_beta_lin_tx_two_yr <- as.matrix(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_w_cov[[2]][
  c("int_2_yr_effect", "lag_num_pd_w_tx"), c("int_2_yr_effect", "lag_num_pd_w_tx")])
attr_deaths_est_log_lin_time_lin_post_tx_2yr <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx_2yr,
                                                                   sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx, 
                                                                   tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"),
                                                                   var_cov_mat_beta_lin_tx_two_yr)
attr_deaths_est_log_lin_time_lin_post_tx_2yr <- merge(attr_deaths_est_log_lin_time_lin_post_tx_2yr, date_data_2yr, 
                                                      by.x = "Time_Period", by.y = "Time_Period_ID")


attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary <- attr_deaths_est_log_lin_time_lin_post_tx_2yr %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(attr_deaths = sum(attr_deaths),
            attr_deaths_lb = sum(attr_deaths_lb),
            attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths)
## [1] 67363.52
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)
## [1] -55713.72
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)
## [1] 190440.8
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] 0.1023426
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] -0.08464353
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] 0.2893287
ggplot(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.5 Linear Policy Measures Post Tx Subset

sens_data_subset <- sensitivity_anlys_event_study_data_lin_post_tx_2yr %>%
  filter(Time_Period_ID <= 30)
#run the gam model
sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr<-gam(log(prop_dead)~ State +
                                                                  s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                  Naloxone_Pharmacy_Yes_Redefined + 
                                                                  lag_num_pd_w_naloxone_yes +
                                                                  Naloxone_Pharmacy_No_Redefined + 
                                                                  lag_num_pd_w_naloxone_no +
                                                                  Medical_Marijuana_Redefined + 
                                                                  lag_num_pd_w_med_marijuana +
                                                                  Recreational_Marijuana_Redefined + 
                                                                  lag_num_pd_w_rec_marijuana +
                                                                  GSL_Redefined + 
                                                                  lag_num_pd_w_gsl +
                                                                  PDMP_Redefined + 
                                                                  lag_num_pd_w_pdmp +
                                                                  Medicaid_Expansion_Redefined +
                                                                  lag_num_pd_w_medicaid +
                                                                  int_2_yr_effect +
                                                                  lag_num_pd_w_tx,
                                                                data = sens_data_subset)
summary(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + int_2_yr_effect + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -10.021226   0.059923 -167.236  < 2e-16 ***
## StateAlaska                        0.443918   0.095754    4.636 3.88e-06 ***
## StateArizona                       0.440959   0.074410    5.926 3.90e-09 ***
## StateArkansas                     -0.389547   0.076027   -5.124 3.41e-07 ***
## StateCalifornia                    0.026610   0.098589    0.270 0.787267    
## StateColorado                      0.249046   0.090536    2.751 0.006022 ** 
## StateConnecticut                   0.475997   0.085658    5.557 3.28e-08 ***
## StateDelaware                      0.220355   0.076352    2.886 0.003961 ** 
## StateFlorida                       0.597255   0.082492    7.240 7.37e-13 ***
## StateGeorgia                       0.137253   0.076182    1.802 0.071816 .  
## StateHawaii                       -0.294001   0.097338   -3.020 0.002570 ** 
## StateIdaho                        -0.214477   0.079981   -2.682 0.007413 ** 
## StateIllinois                      0.339921   0.087098    3.903 9.96e-05 ***
## StateIndiana                      -0.098791   0.079538   -1.242 0.214423    
## StateIowa                         -0.649208   0.073157   -8.874  < 2e-16 ***
## StateKansas                       -0.132411   0.073982   -1.790 0.073709 .  
## StateKentucky                      0.629274   0.079648    7.901 5.58e-15 ***
## StateLouisiana                     0.401089   0.073403    5.464 5.50e-08 ***
## StateMaine                         0.149907   0.094182    1.592 0.111686    
## StateMaryland                     -1.709234   0.078727  -21.711  < 2e-16 ***
## StateMassachusetts                -0.369406   0.080232   -4.604 4.52e-06 ***
## StateMichigan                     -0.038650   0.082294   -0.470 0.638671    
## StateMinnesota                    -0.501390   0.081363   -6.162 9.36e-10 ***
## StateMississippi                   0.029578   0.073196    0.404 0.686205    
## StateMissouri                      0.295634   0.078749    3.754 0.000181 ***
## StateMontana                      -0.170564   0.082931   -2.057 0.039899 *  
## StateNebraska                     -0.771116   0.076030  -10.142  < 2e-16 ***
## StateNevada                        0.594454   0.092584    6.421 1.85e-10 ***
## StateNew Hampshire                 0.161648   0.076915    2.102 0.035763 *  
## StateNew Jersey                    0.088110   0.075402    1.169 0.242786    
## StateNew Mexico                    1.076203   0.096501   11.152  < 2e-16 ***
## StateNew York                     -0.208848   0.083520   -2.501 0.012513 *  
## StateNorth Carolina                0.290062   0.073784    3.931 8.86e-05 ***
## StateNorth Dakota                 -1.214520   0.073058  -16.624  < 2e-16 ***
## StateOhio                          0.580702   0.081050    7.165 1.26e-12 ***
## StateOklahoma                      0.444811   0.079529    5.593 2.68e-08 ***
## StateOregon                        0.119127   0.095563    1.247 0.212757    
## StatePennsylvania                  0.609442   0.085881    7.096 2.03e-12 ***
## StateRhode Island                 -0.529456   0.084963   -6.232 6.10e-10 ***
## StateSouth Carolina                0.233586   0.073378    3.183 0.001488 ** 
## StateSouth Dakota                 -0.992709   0.075504  -13.148  < 2e-16 ***
## StateTennessee                     0.403206   0.073918    5.455 5.79e-08 ***
## StateTexas                        -0.022492   0.079614   -0.283 0.777589    
## StateUtah                         -0.286627   0.079529   -3.604 0.000324 ***
## StateVermont                      -0.076646   0.080670   -0.950 0.342217    
## StateVirginia                     -0.024922   0.076636   -0.325 0.745080    
## StateWashington                    0.384516   0.093990    4.091 4.54e-05 ***
## StateWest Virginia                 0.664839   0.079753    8.336  < 2e-16 ***
## StateWisconsin                     0.142372   0.078046    1.824 0.068333 .  
## StateWyoming                      -0.010003   0.073674   -0.136 0.892015    
## Naloxone_Pharmacy_Yes_Redefined   -0.230136   0.075690   -3.040 0.002406 ** 
## lag_num_pd_w_naloxone_yes         -0.044997   0.023372   -1.925 0.054398 .  
## Naloxone_Pharmacy_No_Redefined     0.147979   0.055249    2.678 0.007484 ** 
## lag_num_pd_w_naloxone_no          -0.042004   0.005732   -7.327 3.95e-13 ***
## Medical_Marijuana_Redefined        0.231345   0.039761    5.818 7.36e-09 ***
## lag_num_pd_w_med_marijuana        -0.015498   0.003114   -4.977 7.24e-07 ***
## Recreational_Marijuana_Redefined  -0.192066   0.188950   -1.016 0.309573    
## lag_num_pd_w_rec_marijuana         0.013613   0.090508    0.150 0.880463    
## GSL_Redefined                      0.127868   0.054818    2.333 0.019812 *  
## lag_num_pd_w_gsl                   0.012098   0.009971    1.213 0.225208    
## PDMP_Redefined                    -0.106932   0.029467   -3.629 0.000295 ***
## lag_num_pd_w_pdmp                  0.013180   0.002810    4.690 3.00e-06 ***
## Medicaid_Expansion_Redefined       0.035884   0.050600    0.709 0.478337    
## lag_num_pd_w_medicaid              0.021458   0.015053    1.425 0.154249    
## int_2_yr_effect                    0.040521   0.024434    1.658 0.097466 .  
## lag_num_pd_w_tx                   -0.019017   0.002933   -6.483 1.24e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   2.699  3.371 115.03  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 7.704  8.567  34.27  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     3.049  3.796  74.54  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      3.067  3.826  39.26  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.831   Deviance explained =   84%
## GCV = 0.084059  Scale est. = 0.079372  n = 1480

6.5.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr <-
  model.matrix(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx_subset_2yr <- coef(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset_w_cov <-
  compute_sd_and_CI_linear_link(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr,
                    log(sens_data_subset$prop_dead),
                    coefficient_values_log_fixed_lin_time_lin_post_tx_subset_2yr,
                    k = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr),
                    print_full_cov = TRUE)
# format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset,4)
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset <- 
  sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset_w_cov[[1]]

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term <-
  rownames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset)


sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_log_2_yr_subset <- sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,]
table_of_RR_lin_eff_log_2_yr_subset$estimate <- round(exp(table_of_RR_lin_eff_log_2_yr_subset$estimate), 3)
table_of_RR_lin_eff_log_2_yr_subset$conf.low <- exp(table_of_RR_lin_eff_log_2_yr_subset$conf.low)
table_of_RR_lin_eff_log_2_yr_subset$conf.high <- exp(table_of_RR_lin_eff_log_2_yr_subset$conf.high)
table_of_RR_lin_eff_log_2_yr_subset$ci_95 <- paste("95% CI = (", 
                                                   format(round(table_of_RR_lin_eff_log_2_yr_subset$conf.low, 3), nsmall = 3), ", ", 
                                                   format(round(table_of_RR_lin_eff_log_2_yr_subset$conf.high, 3), nsmall = 3), ")", sep = "")

# write.csv(table_of_RR_lin_eff_log_2_yr_subset, "./Data/table_of_RR_lin_eff_log_2_yr_subset_5_13_22.csv")
table_of_RR_lin_eff_log_2_yr_subset
##                                   conf.low estimate conf.high          sd
## Naloxone_Pharmacy_Yes_Redefined  0.6233455    0.794 1.0124590 0.123733753
## lag_num_pd_w_naloxone_yes        0.8938476    0.956 1.0224753 0.034297565
## Naloxone_Pharmacy_No_Redefined   0.8470147    1.159 1.5872389 0.160212538
## lag_num_pd_w_naloxone_no         0.9341808    0.959 0.9842032 0.013306744
## Medical_Marijuana_Redefined      0.9841270    1.260 1.6139589 0.126196532
## lag_num_pd_w_med_marijuana       0.9679095    0.985 1.0016215 0.008733889
## Recreational_Marijuana_Redefined 0.5588103    0.825 1.2187355 0.198918139
## lag_num_pd_w_rec_marijuana       0.8732042    1.014 1.1768160 0.076122013
## GSL_Redefined                    0.8940525    1.136 1.4444477 0.122376987
## lag_num_pd_w_gsl                 0.9730732    1.012 1.0528401 0.020098813
## PDMP_Redefined                   0.7664974    0.899 1.0534379 0.081118118
## lag_num_pd_w_pdmp                0.9946137    1.013 1.0322696 0.009479775
## Medicaid_Expansion_Redefined     0.8450536    1.037 1.2714066 0.104203845
## lag_num_pd_w_medicaid            0.9582993    1.022 1.0892729 0.032680004
## int_2_yr_effect                  0.9124951    1.041 1.1884070 0.067394481
## lag_num_pd_w_tx                  0.9672208    0.981 0.9953056 0.007301789
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## int_2_yr_effect                                   int_2_yr_effect
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                    ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.623, 1.012)
## lag_num_pd_w_naloxone_yes        95% CI = (0.894, 1.022)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.847, 1.587)
## lag_num_pd_w_naloxone_no         95% CI = (0.934, 0.984)
## Medical_Marijuana_Redefined      95% CI = (0.984, 1.614)
## lag_num_pd_w_med_marijuana       95% CI = (0.968, 1.002)
## Recreational_Marijuana_Redefined 95% CI = (0.559, 1.219)
## lag_num_pd_w_rec_marijuana       95% CI = (0.873, 1.177)
## GSL_Redefined                    95% CI = (0.894, 1.444)
## lag_num_pd_w_gsl                 95% CI = (0.973, 1.053)
## PDMP_Redefined                   95% CI = (0.766, 1.053)
## lag_num_pd_w_pdmp                95% CI = (0.995, 1.032)
## Medicaid_Expansion_Redefined     95% CI = (0.845, 1.271)
## lag_num_pd_w_medicaid            95% CI = (0.958, 1.089)
## int_2_yr_effect                  95% CI = (0.912, 1.188)
## lag_num_pd_w_tx                  95% CI = (0.967, 0.995)

6.5.2 Plot Effects

plot_data_subset_data <- data.frame(time_after_tx = 0:28,
                                    coef = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                                        sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28),
                                    conf.low = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                                        sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28),
                                    conf.high = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                                        sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28))

ggplot(plot_data_subset_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect, Without Last 5 Years") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

6.5.3 Attributable Deaths

date_data_subset_2yr <- sens_data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset_2yr <- date_data_subset_2yr[!duplicated(date_data_subset_2yr),]
var_cov_mat_beta_lin_tx_two_yr_subset <- as.matrix(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset_w_cov[[2]][
  c("int_2_yr_effect", "lag_num_pd_w_tx"), c("int_2_yr_effect", "lag_num_pd_w_tx")])
attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr <- attr_death_compute(sens_data_subset,
                                                                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset, 
                                                                          tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"),
                                                                          var_cov_mat_beta_lin_tx_two_yr_subset)
attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr <- merge(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr, date_data_subset_2yr, 
                                                             by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary <- attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(attr_deaths = sum(attr_deaths),
            attr_deaths_lb = sum(attr_deaths_lb),
            attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths)
## [1] 36057.52
sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_lb)
## [1] -18032.95
sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_ub)
## [1] 90147.98
sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths)/
  sum(sens_data_subset$imputed_deaths)
## [1] 0.09511979
sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_lb)/
  sum(sens_data_subset$imputed_deaths)
## [1] -0.04757094
sum(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_ub)/
  sum(sens_data_subset$imputed_deaths)
## [1] 0.2378105
ggplot(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.6 Logistic Model

#run the gam model
sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time_2yr<-gam(cbind(round(imputed_deaths), round(num_alive))~ State +
                                                                      s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                      Naloxone_Pharmacy_Yes_Redefined + 
                                                                      lag_num_pd_w_naloxone_yes +
                                                                      Naloxone_Pharmacy_No_Redefined + 
                                                                      lag_num_pd_w_naloxone_no +
                                                                      Medical_Marijuana_Redefined + 
                                                                      lag_num_pd_w_med_marijuana +
                                                                      Recreational_Marijuana_Redefined + 
                                                                      lag_num_pd_w_rec_marijuana +
                                                                      GSL_Redefined + 
                                                                      lag_num_pd_w_gsl +
                                                                      PDMP_Redefined + 
                                                                      lag_num_pd_w_pdmp +
                                                                      Medicaid_Expansion_Redefined +
                                                                      lag_num_pd_w_medicaid +
                                                                      int_2_yr_effect +
                                                                      lag_num_pd_w_tx,
                                                                    data = sensitivity_anlys_event_study_data_lin_post_tx_2yr,
                                                                    family = "binomial")
summary(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time_2yr)
## 
## Family: binomial 
## Link function: logit 
## 
## Formula:
## cbind(round(imputed_deaths), round(num_alive)) ~ State + s(Time_Period_ID, 
##     bs = "cr", by = as.factor(Region)) + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + int_2_yr_effect + lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)                      -9.8321609  0.0154379 -636.884  < 2e-16 ***
## StateAlaska                       0.3218420  0.0307209   10.476  < 2e-16 ***
## StateArizona                      0.3163972  0.0145853   21.693  < 2e-16 ***
## StateArkansas                    -0.3828240  0.0212683  -18.000  < 2e-16 ***
## StateCalifornia                  -0.1045979  0.0180824   -5.785 7.27e-09 ***
## StateColorado                     0.2013450  0.0193117   10.426  < 2e-16 ***
## StateConnecticut                  0.1485688  0.0174318    8.523  < 2e-16 ***
## StateDelaware                     0.4410561  0.0234040   18.845  < 2e-16 ***
## StateFlorida                      0.4159551  0.0144493   28.787  < 2e-16 ***
## StateGeorgia                      0.0402410  0.0148633    2.707  0.00678 ** 
## StateHawaii                      -0.2349882  0.0289223   -8.125 4.48e-16 ***
## StateIdaho                       -0.2199773  0.0248543   -8.851  < 2e-16 ***
## StateIllinois                     0.1388948  0.0163840    8.477  < 2e-16 ***
## StateIndiana                     -0.0086600  0.0156521   -0.553  0.58007    
## StateIowa                        -0.7400136  0.0216308  -34.211  < 2e-16 ***
## StateKansas                      -0.3427351  0.0199412  -17.187  < 2e-16 ***
## StateKentucky                     0.5878146  0.0151191   38.879  < 2e-16 ***
## StateLouisiana                    0.3355941  0.0144858   23.167  < 2e-16 ***
## StateMaine                        0.3049901  0.0262796   11.606  < 2e-16 ***
## StateMaryland                    -1.0546437  0.0212378  -49.659  < 2e-16 ***
## StateMassachusetts                0.1145138  0.0152728    7.498 6.48e-14 ***
## StateMichigan                    -0.0022631  0.0163667   -0.138  0.89002    
## StateMinnesota                   -0.5426573  0.0197609  -27.461  < 2e-16 ***
## StateMississippi                 -0.1307151  0.0182650   -7.157 8.27e-13 ***
## StateMissouri                     0.3156684  0.0188963   16.705  < 2e-16 ***
## StateMontana                     -0.2073809  0.0311345   -6.661 2.72e-11 ***
## StateNebraska                    -0.9072521  0.0295071  -30.747  < 2e-16 ***
## StateNevada                       0.4570621  0.0203505   22.459  < 2e-16 ***
## StateNew Hampshire                0.3587530  0.0214050   16.760  < 2e-16 ***
## StateNew Jersey                   0.1508292  0.0143974   10.476  < 2e-16 ***
## StateNew Mexico                   0.5461757  0.0194534   28.076  < 2e-16 ***
## StateNew York                    -0.3575329  0.0144383  -24.763  < 2e-16 ***
## StateNorth Carolina               0.2171845  0.0129294   16.798  < 2e-16 ***
## StateNorth Dakota                -1.1155014  0.0455566  -24.486  < 2e-16 ***
## StateOhio                         0.5910244  0.0140476   42.073  < 2e-16 ***
## StateOklahoma                     0.3578483  0.0163306   21.913  < 2e-16 ***
## StateOregon                       0.0307509  0.0222936    1.379  0.16778    
## StatePennsylvania                 0.5409898  0.0154475   35.021  < 2e-16 ***
## StateRhode Island                 0.2405995  0.0239772   10.035  < 2e-16 ***
## StateSouth Carolina               0.1931136  0.0147057   13.132  < 2e-16 ***
## StateSouth Dakota                -0.9781675  0.0429151  -22.793  < 2e-16 ***
## StateTennessee                    0.4204731  0.0133469   31.503  < 2e-16 ***
## StateTexas                       -0.1995416  0.0140762  -14.176  < 2e-16 ***
## StateUtah                        -0.0042427  0.0194295   -0.218  0.82714    
## StateVermont                     -0.0742320  0.0325325   -2.282  0.02250 *  
## StateVirginia                    -0.0385264  0.0149442   -2.578  0.00994 ** 
## StateWashington                   0.1786400  0.0192851    9.263  < 2e-16 ***
## StateWest Virginia                0.7574308  0.0168606   44.923  < 2e-16 ***
## StateWisconsin                    0.0861369  0.0160543    5.365 8.08e-08 ***
## StateWyoming                     -0.0217045  0.0338027   -0.642  0.52081    
## Naloxone_Pharmacy_Yes_Redefined  -0.0127335  0.0080392   -1.584  0.11321    
## lag_num_pd_w_naloxone_yes        -0.0248185  0.0011149  -22.261  < 2e-16 ***
## Naloxone_Pharmacy_No_Redefined    0.0532019  0.0079497    6.692 2.20e-11 ***
## lag_num_pd_w_naloxone_no         -0.0025223  0.0009291   -2.715  0.00663 ** 
## Medical_Marijuana_Redefined       0.0499353  0.0058335    8.560  < 2e-16 ***
## lag_num_pd_w_med_marijuana       -0.0060568  0.0006409   -9.450  < 2e-16 ***
## Recreational_Marijuana_Redefined -0.0022378  0.0101085   -0.221  0.82480    
## lag_num_pd_w_rec_marijuana       -0.0123077  0.0019374   -6.353 2.12e-10 ***
## GSL_Redefined                     0.0328623  0.0061094    5.379 7.49e-08 ***
## lag_num_pd_w_gsl                  0.0126428  0.0008491   14.890  < 2e-16 ***
## PDMP_Redefined                   -0.0087167  0.0068770   -1.268  0.20497    
## lag_num_pd_w_pdmp                 0.0062363  0.0006394    9.753  < 2e-16 ***
## Medicaid_Expansion_Redefined      0.0673463  0.0067767    9.938  < 2e-16 ***
## lag_num_pd_w_medicaid             0.0174025  0.0010348   16.818  < 2e-16 ***
## int_2_yr_effect                   0.0501628  0.0047647   10.528  < 2e-16 ***
## lag_num_pd_w_tx                  -0.0092812  0.0003685  -25.184  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df Chi.sq p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   8.901  8.997   4861  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.938  8.999   3891  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     8.912  8.998   4306  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      8.749  8.980   1773  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.917   Deviance explained = 91.2%
## UBRE = 7.8405  Scale est. = 1         n = 1980
plot(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time_2yr, pages = 1)

6.6.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_2_yr <- model.matrix(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time_2yr)

#estimate the 95% CI and SD
coefficient_values_logistic_fixed_lin_time_lin_post_tx_2yr <- coef(sensitivity_anlys_lin_post_tx_model_logistic_smoothed_time_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_w_cov <-
  compute_sd_and_CI_logistic(full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_2_yr,
                    (sensitivity_anlys_event_study_data_lin_post_tx_2yr$prop_dead),
                    coefficient_values_logistic_fixed_lin_time_lin_post_tx_2yr,
                    k = ncol(full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_2_yr),
                    print_full_cov = TRUE)
# format(round(main_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx,4)
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx <- sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_w_cov[[1]]

colnames(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx) <- c("conf.low", "estimate", "conf.high", "exp_lb",
                                                                           "exp_coef", "exp_ub", "sd")
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term <- rownames(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx)




sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention") + 
  scale_color_grey()      + 
  geom_text(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.3, 1.1)

table_of_RR_lin_eff_logistic_2_yr<- sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx[51:66,]
table_of_RR_lin_eff_logistic_2_yr$estimate <- round(exp(table_of_RR_lin_eff_logistic_2_yr$estimate), 3)
table_of_RR_lin_eff_logistic_2_yr$conf.low <- exp(table_of_RR_lin_eff_logistic_2_yr$conf.low)
table_of_RR_lin_eff_logistic_2_yr$conf.high <- exp(table_of_RR_lin_eff_logistic_2_yr$conf.high)
table_of_RR_lin_eff_logistic_2_yr$ci_95 <- paste("95% CI = (", 
                                                 format(round(table_of_RR_lin_eff_logistic_2_yr$conf.low, 3), nsmall = 3), ", ", 
                                                 format(round(table_of_RR_lin_eff_logistic_2_yr$conf.high, 3), nsmall = 3), ")", sep = "")

# write.csv(table_of_RR_lin_eff_logistic_2_yr, "./Data/table_of_RR_lin_eff_logistic_2_yr_5_13_22.csv")
table_of_RR_lin_eff_logistic_2_yr
##                                   conf.low estimate conf.high    exp_lb
## Naloxone_Pharmacy_Yes_Redefined  0.8419925    0.987  1.157795 0.8419925
## lag_num_pd_w_naloxone_yes        0.9466751    0.975  1.005176 0.9466751
## Naloxone_Pharmacy_No_Redefined   0.8618881    1.055  1.290505 0.8618881
## lag_num_pd_w_naloxone_no         0.9821647    0.997  1.013036 0.9821647
## Medical_Marijuana_Redefined      0.8624636    1.051  1.281246 0.8624636
## lag_num_pd_w_med_marijuana       0.9746480    0.994  1.013658 0.9746480
## Recreational_Marijuana_Redefined 0.8052455    0.998  1.236312 0.8052455
## lag_num_pd_w_rec_marijuana       0.9566606    0.988  1.019886 0.9566606
## GSL_Redefined                    0.9163945    1.033  1.165363 0.9163945
## lag_num_pd_w_gsl                 0.9915922    1.013  1.034304 0.9915922
## PDMP_Redefined                   0.7716857    0.991  1.273469 0.7716857
## lag_num_pd_w_pdmp                0.9793430    1.006  1.033908 0.9793430
## Medicaid_Expansion_Redefined     0.9355746    1.070  1.222976 0.9355746
## lag_num_pd_w_medicaid            0.9941450    1.018  1.041516 0.9941450
## int_2_yr_effect                  0.9310482    1.051  1.187404 0.9310482
## lag_num_pd_w_tx                  0.9769565    0.991  1.004762 0.9769565
##                                   exp_coef   exp_ub          sd
## Naloxone_Pharmacy_Yes_Redefined  0.9873472 1.157795 0.081250304
## lag_num_pd_w_naloxone_yes        0.9754870 1.005176 0.015296341
## Naloxone_Pharmacy_No_Redefined   1.0546426 1.290505 0.102975373
## lag_num_pd_w_naloxone_no         0.9974809 1.013036 0.007894884
## Medical_Marijuana_Redefined      1.0512031 1.281246 0.100968173
## lag_num_pd_w_med_marijuana       0.9939615 1.013658 0.010011282
## Recreational_Marijuana_Redefined 0.9977647 1.236312 0.109372599
## lag_num_pd_w_rec_marijuana       0.9877677 1.019886 0.016325970
## GSL_Redefined                    1.0334082 1.165363 0.061311521
## lag_num_pd_w_gsl                 1.0127230 1.034304 0.010758215
## PDMP_Redefined                   0.9913212 1.273469 0.127786349
## lag_num_pd_w_pdmp                1.0062558 1.033908 0.013831423
## Medicaid_Expansion_Redefined     1.0696658 1.222976 0.068337078
## lag_num_pd_w_medicaid            1.0175548 1.041516 0.011874861
## int_2_yr_effect                  1.0514422 1.187404 0.062044370
## lag_num_pd_w_tx                  0.9907617 1.004762 0.007159133
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## int_2_yr_effect                                   int_2_yr_effect
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                    ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.842, 1.158)
## lag_num_pd_w_naloxone_yes        95% CI = (0.947, 1.005)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.862, 1.291)
## lag_num_pd_w_naloxone_no         95% CI = (0.982, 1.013)
## Medical_Marijuana_Redefined      95% CI = (0.862, 1.281)
## lag_num_pd_w_med_marijuana       95% CI = (0.975, 1.014)
## Recreational_Marijuana_Redefined 95% CI = (0.805, 1.236)
## lag_num_pd_w_rec_marijuana       95% CI = (0.957, 1.020)
## GSL_Redefined                    95% CI = (0.916, 1.165)
## lag_num_pd_w_gsl                 95% CI = (0.992, 1.034)
## PDMP_Redefined                   95% CI = (0.772, 1.273)
## lag_num_pd_w_pdmp                95% CI = (0.979, 1.034)
## Medicaid_Expansion_Redefined     95% CI = (0.936, 1.223)
## lag_num_pd_w_medicaid            95% CI = (0.994, 1.042)
## int_2_yr_effect                  95% CI = (0.931, 1.187)
## lag_num_pd_w_tx                  95% CI = (0.977, 1.005)

6.6.2 Plot Effects

plot_data <- data.frame(time_after_tx = 0:38,
                        coef = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                            sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.low = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                            sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.high = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                            sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38))

ggplot(plot_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

6.6.3 Attributable Deaths

date_data_2yr <- sensitivity_anlys_event_study_data_lin_post_tx_2yr[, c("Time_Period_ID", "Time_Period_Start")]
date_data_2yr <- date_data_2yr[!duplicated(date_data_2yr),]
var_cov_mat_beta_logistic_tx_two_yr <- as.matrix(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_w_cov[[2]][
  c("int_2_yr_effect", "lag_num_pd_w_tx"), c("int_2_yr_effect", "lag_num_pd_w_tx")])
attr_deaths_est_logistic_lin_time_lin_post_tx_2yr <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx_2yr,
                                                                        sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx, 
                                                                        tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"),
                                                                        var_cov_mat_beta_logistic_tx_two_yr)
attr_deaths_est_logistic_lin_time_lin_post_tx_2yr <- merge(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr, date_data_2yr, 
                                                           by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary <- attr_deaths_est_logistic_lin_time_lin_post_tx_2yr %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(attr_deaths = sum(attr_deaths),
            attr_deaths_lb = sum(attr_deaths_lb),
            attr_deaths_ub = sum(attr_deaths_ub))

sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths)
## [1] 45316.34
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)
## [1] -103213.9
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)
## [1] 193846.5
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] 0.06884724
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] -0.1568085
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)/
  sum(sensitivity_anlys_event_study_data_lin_post_tx_2yr$imputed_deaths)
## [1] 0.294503
ggplot(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.7 Linear Policy Measures Post Tx Subset Logistic Model

sens_data_subset <- sensitivity_anlys_event_study_data_lin_post_tx_2yr %>%
  filter(Time_Period_ID <= 30)
#run the gam model
sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr<-gam(cbind(round(imputed_deaths), round(num_alive))~ State +
                                                                  s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                  Naloxone_Pharmacy_Yes_Redefined + 
                                                                  lag_num_pd_w_naloxone_yes +
                                                                  Naloxone_Pharmacy_No_Redefined + 
                                                                  lag_num_pd_w_naloxone_no +
                                                                  Medical_Marijuana_Redefined + 
                                                                  lag_num_pd_w_med_marijuana +
                                                                  Recreational_Marijuana_Redefined + 
                                                                  lag_num_pd_w_rec_marijuana +
                                                                  GSL_Redefined + 
                                                                  lag_num_pd_w_gsl +
                                                                  PDMP_Redefined + 
                                                                  lag_num_pd_w_pdmp +
                                                                  Medicaid_Expansion_Redefined +
                                                                  lag_num_pd_w_medicaid +
                                                                  int_2_yr_effect +
                                                                  lag_num_pd_w_tx,
                                                                data = sens_data_subset,
                                                                family = "binomial")
summary(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
## 
## Family: binomial 
## Link function: logit 
## 
## Formula:
## cbind(round(imputed_deaths), round(num_alive)) ~ State + s(Time_Period_ID, 
##     bs = "cr", by = as.factor(Region)) + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + int_2_yr_effect + lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)                      -9.9940920  0.0165638 -603.368  < 2e-16 ***
## StateAlaska                       0.5704251  0.0389754   14.636  < 2e-16 ***
## StateArizona                      0.3903527  0.0178959   21.812  < 2e-16 ***
## StateArkansas                    -0.3055744  0.0261935  -11.666  < 2e-16 ***
## StateCalifornia                   0.0199033  0.0243552    0.817  0.41381    
## StateColorado                     0.3072100  0.0253506   12.118  < 2e-16 ***
## StateConnecticut                  0.2647991  0.0227014   11.664  < 2e-16 ***
## StateDelaware                     0.2648217  0.0325417    8.138 4.02e-16 ***
## StateFlorida                      0.5227044  0.0179353   29.144  < 2e-16 ***
## StateGeorgia                      0.0919452  0.0184768    4.976 6.48e-07 ***
## StateHawaii                      -0.2559009  0.0380658   -6.723 1.79e-11 ***
## StateIdaho                       -0.2913733  0.0321544   -9.062  < 2e-16 ***
## StateIllinois                     0.1775955  0.0201036    8.834  < 2e-16 ***
## StateIndiana                     -0.1166725  0.0203197   -5.742 9.37e-09 ***
## StateIowa                        -0.6980084  0.0275671  -25.320  < 2e-16 ***
## StateKansas                      -0.2393622  0.0246235   -9.721  < 2e-16 ***
## StateKentucky                     0.5731072  0.0193495   29.619  < 2e-16 ***
## StateLouisiana                    0.3555001  0.0183982   19.323  < 2e-16 ***
## StateMaine                        0.1515154  0.0349014    4.341 1.42e-05 ***
## StateMaryland                    -1.4791166  0.0319252  -46.331  < 2e-16 ***
## StateMassachusetts               -0.0268117  0.0199714   -1.343  0.17943    
## StateMichigan                    -0.0516862  0.0202606   -2.551  0.01074 *  
## StateMinnesota                   -0.4853177  0.0251070  -19.330  < 2e-16 ***
## StateMississippi                 -0.0331251  0.0224995   -1.472  0.14095    
## StateMissouri                     0.2578009  0.0207321   12.435  < 2e-16 ***
## StateMontana                     -0.0932257  0.0381194   -2.446  0.01446 *  
## StateNebraska                    -0.8087276  0.0368186  -21.965  < 2e-16 ***
## StateNevada                       0.6116326  0.0262068   23.339  < 2e-16 ***
## StateNew Hampshire                0.2002867  0.0288652    6.939 3.96e-12 ***
## StateNew Jersey                   0.0784250  0.0180615    4.342 1.41e-05 ***
## StateNew Mexico                   0.9356101  0.0256184   36.521  < 2e-16 ***
## StateNew York                    -0.3150371  0.0188756  -16.690  < 2e-16 ***
## StateNorth Carolina               0.2099208  0.0167790   12.511  < 2e-16 ***
## StateNorth Dakota                -1.2635431  0.0665977  -18.973  < 2e-16 ***
## StateOhio                         0.5089304  0.0177118   28.734  < 2e-16 ***
## StateOklahoma                     0.4048662  0.0203332   19.912  < 2e-16 ***
## StateOregon                       0.1447300  0.0289562    4.998 5.79e-07 ***
## StatePennsylvania                 0.5029050  0.0194993   25.791  < 2e-16 ***
## StateRhode Island                 0.1444098  0.0319644    4.518 6.25e-06 ***
## StateSouth Carolina               0.1823589  0.0189975    9.599  < 2e-16 ***
## StateSouth Dakota                -0.9878693  0.0564320  -17.505  < 2e-16 ***
## StateTennessee                    0.3444484  0.0172974   19.913  < 2e-16 ***
## StateTexas                       -0.1383717  0.0176081   -7.858 3.89e-15 ***
## StateUtah                        -0.1365666  0.0257712   -5.299 1.16e-07 ***
## StateVermont                     -0.0597918  0.0434224   -1.377  0.16852    
## StateVirginia                    -0.1419516  0.0192643   -7.369 1.72e-13 ***
## StateWashington                   0.4295969  0.0253198   16.967  < 2e-16 ***
## StateWest Virginia                0.7023825  0.0218277   32.178  < 2e-16 ***
## StateWisconsin                    0.0803056  0.0204144    3.934 8.36e-05 ***
## StateWyoming                      0.0234258  0.0416361    0.563  0.57368    
## Naloxone_Pharmacy_Yes_Redefined  -0.1017463  0.0137698   -7.389 1.48e-13 ***
## lag_num_pd_w_naloxone_yes        -0.0266718  0.0036787   -7.250 4.16e-13 ***
## Naloxone_Pharmacy_No_Redefined    0.0879798  0.0106238    8.281  < 2e-16 ***
## lag_num_pd_w_naloxone_no         -0.0132960  0.0012389  -10.732  < 2e-16 ***
## Medical_Marijuana_Redefined       0.0251814  0.0101224    2.488  0.01286 *  
## lag_num_pd_w_med_marijuana       -0.0073101  0.0010646   -6.866 6.59e-12 ***
## Recreational_Marijuana_Redefined -0.0759374  0.0382852   -1.983  0.04732 *  
## lag_num_pd_w_rec_marijuana       -0.0263943  0.0185488   -1.423  0.15475    
## GSL_Redefined                    -0.0305314  0.0103521   -2.949  0.00319 ** 
## lag_num_pd_w_gsl                  0.0009969  0.0025707    0.388  0.69816    
## PDMP_Redefined                   -0.0083421  0.0078626   -1.061  0.28870    
## lag_num_pd_w_pdmp                 0.0092754  0.0008061   11.507  < 2e-16 ***
## Medicaid_Expansion_Redefined      0.0262864  0.0104455    2.517  0.01185 *  
## lag_num_pd_w_medicaid             0.0208138  0.0029797    6.985 2.85e-12 ***
## int_2_yr_effect                   0.0396273  0.0055026    7.202 5.95e-13 ***
## lag_num_pd_w_tx                  -0.0148389  0.0005735  -25.874  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df Chi.sq p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   8.485  8.919   3307  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.693  8.971   1828  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     8.687  8.970   3288  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      8.916  8.998   1254  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.88   Deviance explained = 87.1%
## UBRE = 5.6567  Scale est. = 1         n = 1480

6.7.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_subset_2yr <-
  model.matrix(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)

#estimate the 95% CI and SD
coefficient_values_logistic_fixed_lin_time_lin_post_tx_subset_2yr <- coef(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset_w_cov <-
  compute_sd_and_CI_logistic(full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_subset_2yr,
                    (sens_data_subset$prop_dead),
                    coefficient_values_logistic_fixed_lin_time_lin_post_tx_subset_2yr,
                    k = ncol(full_df_w_basis_functions_logistic_fixed_lin_time_lin_post_tx_subset_2yr),
                    print_full_cov = TRUE)
# format(round(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset,4)
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset <-
  sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset_w_cov[[1]]

colnames(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset) <- c("conf.low", "estimate", "conf.high", "exp_lb",
                                                                                  "exp_coef", "exp_ub", "sd")
sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term <-
  rownames(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset)


sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.high, 3), nsmall = 3), ")", sep = "")

dwplot(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

table_of_RR_lin_eff_logistic_2_yr_subset<- sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset[51:66,]
table_of_RR_lin_eff_logistic_2_yr_subset$estimate <- round(exp(table_of_RR_lin_eff_logistic_2_yr_subset$estimate), 3)
table_of_RR_lin_eff_logistic_2_yr_subset$conf.low <- exp(table_of_RR_lin_eff_logistic_2_yr_subset$conf.low)
table_of_RR_lin_eff_logistic_2_yr_subset$conf.high <- exp(table_of_RR_lin_eff_logistic_2_yr_subset$conf.high)
table_of_RR_lin_eff_logistic_2_yr_subset$ci_95 <- paste("95% CI = (", 
                                                        format(round(table_of_RR_lin_eff_logistic_2_yr_subset$conf.low, 4), nsmall = 4), 
                                                        ", ", 
                                                        format(round(table_of_RR_lin_eff_logistic_2_yr_subset$conf.high, 4), nsmall = 4),
                                                        ")", sep = "")

# write.csv(table_of_RR_lin_eff_logistic_2_yr_subset, "./Data/table_of_RR_lin_eff_logistic_2_yr_subset_5_13_22.csv")
table_of_RR_lin_eff_logistic_2_yr_subset
##                                   conf.low estimate conf.high    exp_lb
## Naloxone_Pharmacy_Yes_Redefined  0.7508905    0.903 1.0865448 0.7508905
## lag_num_pd_w_naloxone_yes        0.9258878    0.974 1.0239408 0.9258878
## Naloxone_Pharmacy_No_Redefined   0.8568023    1.092 1.3916747 0.8568023
## lag_num_pd_w_naloxone_no         0.9713329    0.987 1.0024971 0.9713329
## Medical_Marijuana_Redefined      0.8457659    1.026 1.2434322 0.8457659
## lag_num_pd_w_med_marijuana       0.9763238    0.993 1.0093846 0.9763238
## Recreational_Marijuana_Redefined 0.6944040    0.927 1.2371701 0.6944040
## lag_num_pd_w_rec_marijuana       0.8705066    0.974 1.0896878 0.8705066
## GSL_Redefined                    0.8401498    0.970 1.1197577 0.8401498
## lag_num_pd_w_gsl                 0.9762945    1.001 1.0263253 0.9762945
## PDMP_Redefined                   0.8423138    0.992 1.1675628 0.8423138
## lag_num_pd_w_pdmp                0.9884350    1.009 1.0306433 0.9884350
## Medicaid_Expansion_Redefined     0.8881937    1.027 1.1866546 0.8881937
## lag_num_pd_w_medicaid            0.9760411    1.021 1.0680967 0.9760411
## int_2_yr_effect                  0.9323847    1.040 1.1609800 0.9323847
## lag_num_pd_w_tx                  0.9712003    0.985 0.9995448 0.9712003
##                                   exp_coef    exp_ub          sd
## Naloxone_Pharmacy_Yes_Redefined  0.9032587 1.0865448 0.094259749
## lag_num_pd_w_naloxone_yes        0.9736808 1.0239408 0.025678813
## Naloxone_Pharmacy_No_Redefined   1.0919661 1.3916747 0.123738759
## lag_num_pd_w_naloxone_no         0.9867920 1.0024971 0.008056117
## Medical_Marijuana_Redefined      1.0255011 1.2434322 0.098313292
## lag_num_pd_w_med_marijuana       0.9927166 1.0093846 0.008495385
## Recreational_Marijuana_Redefined 0.9268742 1.2371701 0.147328556
## lag_num_pd_w_rec_marijuana       0.9739509 1.0896878 0.057288563
## GSL_Redefined                    0.9699300 1.1197577 0.073287584
## lag_num_pd_w_gsl                 1.0009974 1.0263253 0.012748920
## PDMP_Redefined                   0.9916926 1.1675628 0.083296223
## lag_num_pd_w_pdmp                1.0093185 1.0306433 0.010667228
## Medicaid_Expansion_Redefined     1.0266349 1.1866546 0.073903958
## lag_num_pd_w_medicaid            1.0210319 1.0680967 0.022992068
## int_2_yr_effect                  1.0404230 1.1609800 0.055937326
## lag_num_pd_w_tx                  0.9852706 0.9995448 0.007338566
##                                                              term
## Naloxone_Pharmacy_Yes_Redefined   Naloxone_Pharmacy_Yes_Redefined
## lag_num_pd_w_naloxone_yes               lag_num_pd_w_naloxone_yes
## Naloxone_Pharmacy_No_Redefined     Naloxone_Pharmacy_No_Redefined
## lag_num_pd_w_naloxone_no                 lag_num_pd_w_naloxone_no
## Medical_Marijuana_Redefined           Medical_Marijuana_Redefined
## lag_num_pd_w_med_marijuana             lag_num_pd_w_med_marijuana
## Recreational_Marijuana_Redefined Recreational_Marijuana_Redefined
## lag_num_pd_w_rec_marijuana             lag_num_pd_w_rec_marijuana
## GSL_Redefined                                       GSL_Redefined
## lag_num_pd_w_gsl                                 lag_num_pd_w_gsl
## PDMP_Redefined                                     PDMP_Redefined
## lag_num_pd_w_pdmp                               lag_num_pd_w_pdmp
## Medicaid_Expansion_Redefined         Medicaid_Expansion_Redefined
## lag_num_pd_w_medicaid                       lag_num_pd_w_medicaid
## int_2_yr_effect                                   int_2_yr_effect
## lag_num_pd_w_tx                                   lag_num_pd_w_tx
##                                                      ci_95
## Naloxone_Pharmacy_Yes_Redefined  95% CI = (0.7509, 1.0865)
## lag_num_pd_w_naloxone_yes        95% CI = (0.9259, 1.0239)
## Naloxone_Pharmacy_No_Redefined   95% CI = (0.8568, 1.3917)
## lag_num_pd_w_naloxone_no         95% CI = (0.9713, 1.0025)
## Medical_Marijuana_Redefined      95% CI = (0.8458, 1.2434)
## lag_num_pd_w_med_marijuana       95% CI = (0.9763, 1.0094)
## Recreational_Marijuana_Redefined 95% CI = (0.6944, 1.2372)
## lag_num_pd_w_rec_marijuana       95% CI = (0.8705, 1.0897)
## GSL_Redefined                    95% CI = (0.8401, 1.1198)
## lag_num_pd_w_gsl                 95% CI = (0.9763, 1.0263)
## PDMP_Redefined                   95% CI = (0.8423, 1.1676)
## lag_num_pd_w_pdmp                95% CI = (0.9884, 1.0306)
## Medicaid_Expansion_Redefined     95% CI = (0.8882, 1.1867)
## lag_num_pd_w_medicaid            95% CI = (0.9760, 1.0681)
## int_2_yr_effect                  95% CI = (0.9324, 1.1610)
## lag_num_pd_w_tx                  95% CI = (0.9712, 0.9995)

6.7.2 Plot Effects

plot_data_subset_data <- data.frame(time_after_tx = 0:28,
                                    coef = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$estimate[
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$estimate[
                                        sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term ==
                                          "lag_num_pd_w_tx"]*c(0:28),
                                    conf.low = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.low[
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.low[
                                        sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term ==
                                          "lag_num_pd_w_tx"]*c(0:28),
                                    conf.high = sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.high[
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                      sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.high[
                                        sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term ==
                                          "lag_num_pd_w_tx"]*c(0:28))

ggplot(plot_data_subset_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect, Without Last 5 Years") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

6.7.3 Attributable Deaths

date_data_subset_2yr <- sens_data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset_2yr <- date_data_subset_2yr[!duplicated(date_data_subset_2yr),]
var_cov_mat_beta_logistic_tx_two_yr_subset <- as.matrix(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset_w_cov[[2]][
  c("int_2_yr_effect", "lag_num_pd_w_tx"), c("int_2_yr_effect", "lag_num_pd_w_tx")])
attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr <- attr_death_compute(sens_data_subset,
                                                                        sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset,
                                                                        tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"),
                                                                        var_cov_mat_beta_logistic_tx_two_yr_subset)
attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr <- merge(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr,
                                                                  date_data_subset_2yr, 
                                                                  by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary <- attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(attr_deaths = sum(attr_deaths),
            attr_deaths_ub = sum(attr_deaths_ub),
            attr_deaths_lb = sum(attr_deaths_lb))

sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths)
## [1] 25158.84
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_lb)
## [1] -23262.85
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_ub)
## [1] 73580.53
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths)/
  sum(sens_data_subset$imputed_deaths)
## [1] 0.06636907
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_lb)/
  sum(sens_data_subset$imputed_deaths)
## [1] -0.06136742
sum(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary$attr_deaths_ub)/
  sum(sens_data_subset$imputed_deaths)
## [1] 0.1941056
ggplot(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7 Plot All Effects

7.1 All Years

plot_effects_all_yr_data <- data.frame(time_after_tx = rep(0:39, 4),
                                       coef = c(
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$estimate[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$estimate[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$estimate[
                                           sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$estimate[
                                               sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$estimate[
                                           sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$estimate[
                                               sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       conf.low = c(
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.low[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.low[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.low[
                                           sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.low[
                                               sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.low[
                                           sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.low[
                                               sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       conf.high = c(
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.high[
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                           sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.high[
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.high[
                                           sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.high[
                                               sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.high[
                                           sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "Intervention_Redefined"] + 
                                             sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$conf.high[
                                               sensitivity_anlys_post_tx_sd_and_ci_logistic_smoothed_time_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       estimator = c(rep("Log Model with All Time Periods", 40),
                                                     rep("Logistic GAM with All Time Periods", 40),
                                                     rep("Log Model with Subset Time Periods", 40),
                                                     rep("Logistic GAM with Subset Time Periods", 40)))

pdf("./Figures/all_yr_effects_all_models_5_13_22.pdf")
ggplot(plot_effects_all_yr_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect", color = estimator)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI", color = estimator)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI", color = estimator)) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")  + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))
dev.off() 
## quartz_off_screen 
##                 2
attr_deaths_all_yr <- rbind(cbind(attr_deaths_est_log_smoothed_time_lin_post_summary, 
                                  "estimator" = "Log Model with All Time Periods"),
                            cbind(attr_deaths_est_logistic_smoothed_time_lin_post_summary, 
                                  "estimator" = "Logistic GAM with All Time Periods"),
                            cbind(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
                                  "estimator" = "Log Model with Subset Time Periods"),
                            cbind(attr_deaths_est_logistic_smoothed_time_lin_post_subset_summary, 
                                  "estimator" = "Logistc GAM with Subset Time Periods"))


pdf("./Figures/lives_saved_all_yr_5_13_22.pdf")
ggplot(attr_deaths_all_yr, aes(x = year, color = estimator)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       # title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       # Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "",
       color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid"))+ 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))
dev.off()
## quartz_off_screen 
##                 2

7.2 Two Years

plot_effects_two_yr_data <- data.frame(time_after_tx = rep(0:39, 4),
                                       coef = c(
                                         sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$estimate[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$estimate[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       conf.low = c(
                                         sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.low[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.low[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       conf.high = c(
                                         sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                         c(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                                           sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28)),
                                         c(sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.high[
                                           sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$conf.high[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28), 
                                           rep(NA, 39 - 28))),
                                       estimator = c(rep("Log Model with All Time Periods", 40),
                                                     rep("Logistic GAM with All Time Periods", 40),
                                                     rep("Log Model with Subset Time Periods", 40),
                                                     rep("Logistic GAM with Subset Time Periods", 40)))

pdf("./Figures/two_yr_effects_all_models_5_13_22.pdf")
ggplot(plot_effects_two_yr_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect", color = estimator)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI", color = estimator)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI", color = estimator)) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")  + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))
dev.off() 
## quartz_off_screen 
##                 2
attr_deaths_all_2yr <- rbind(cbind(attr_deaths_est_log_lin_time_lin_post_tx_2yr, "estimator" = "Log Model with All Time Periods"),
                             cbind(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr, "estimator" = "Logistic GAM with All Time Periods"),
                             cbind(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr, 
                                   "estimator" = "Log Model with Subset Time Periods"),
                             cbind(attr_deaths_est_logistic_lin_time_lin_post_tx_subset_2yr, 
                                   "estimator" = "Logistc GAM with Subset Time Periods"))


pdf("./Figures/lives_saved_all_2_yr_5_13_22.pdf")
ggplot(attr_deaths_all_2yr, aes(x = Time_Period_Start, color = estimator)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       # title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       # Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "",
       color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid"))+ 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))
dev.off()
## quartz_off_screen 
##                 2

7.3 Log and Logistic Model All Years

plot_effects_log_all_yr_data <- data.frame(time_after_tx = rep(0:39, 2),
                                           coef = c(
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                                 sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$estimate[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$estimate[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$estimate[
                                                 sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39)),
                                           conf.low = c(
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                                                 sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.low[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.low[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.low[
                                                 sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39)),
                                           conf.high = c(
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                                                 sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.high[
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "Intervention_Redefined"] + 
                                               sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$conf.high[
                                                 sensitivity_anlys_lin_post_tx_sd_and_ci_logistic_smoothed_time$term == "lag_num_pd_w_tx"]*c(0:39),
                                             sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                               sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$conf.high[
                                                 sens_analysis_sd_and_ci_logistic_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:39)),
                                           model = c(rep("Log GAM: Lasting Effect", 40),
                                                     rep("Log GAM: Two Year Effect", 40),
                                                     rep("Logistic GAM: Lasting Effect", 40),
                                                     rep("Logistic GAM: Two Year Effect", 40)))

pdf("./Figures/log_and_logistic_models_all_yr_effects_5_13_22_RR.pdf")
ggplot(plot_effects_log_all_yr_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = exp(coef), linetype = "Estimated Effect", color = model)) +
  geom_point(aes(y = exp(coef), linetype = "Estimated Effect", shape = model, color = model)) +
  geom_line(aes(y = exp(conf.low), linetype = "95% CI", color = model)) + 
  geom_line(aes(y = exp(conf.high), linetype = "95% CI", color = model)) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Risk Ratio",
       color = "", shape = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 1), color = "grey", linetype = "dotted")  + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE),
         shape = guide_legend(nrow=2,byrow=TRUE)) + 
  scale_shape_manual(values = c(19, 4, 17, 5))
dev.off() 
## quartz_off_screen 
##                 2
attr_deaths_est_log_lin_time_lin_post_tx_2yr_wo_last_col <-
  attr_deaths_est_log_lin_time_lin_post_tx_2yr[,c(1:(ncol(attr_deaths_est_log_lin_time_lin_post_tx_2yr) -1))]
attr_deaths_est_log_lin_time_lin_post_tx_2yr_wo_last_col$Time_Period <-
  year(attr_deaths_est_log_lin_time_lin_post_tx_2yr$Time_Period_Start)

attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_wo_last_col <-
  attr_deaths_est_logistic_lin_time_lin_post_tx_2yr[,c(1:(ncol(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr) -1))]
attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_wo_last_col$Time_Period <- 
  year(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr$Time_Period_Start)

colnames(attr_deaths_est_log_smoothed_time_lin_post_summary) <- 
  colnames(attr_deaths_est_logistic_smoothed_time_lin_post_summary) <-
  colnames(attr_deaths_est_log_lin_time_lin_post_tx_2yr_wo_last_col) <-
  colnames(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_wo_last_col) <-
  c("Time_Period", "attr_deaths", "attr_deaths_lb", "attr_deaths_ub")

attr_deaths_all_yr_effect <- rbind(cbind(attr_deaths_est_log_smoothed_time_lin_post_summary, "model" = "Log Model: Lasting Effect"),
                                   cbind(attr_deaths_est_logistic_smoothed_time_lin_post_summary, "model" = "Logistic GAM: Lasting Effect"),
                                   cbind(attr_deaths_est_log_lin_time_lin_post_tx_2yr_wo_last_col, "model" = "Log Model: Two Year Effect"),
                                   cbind(attr_deaths_est_logistic_lin_time_lin_post_tx_2yr_wo_last_col, 
                                         "model" = "Logistic GAM: Two Year Effect"))


pdf("./Figures/lives_saved_main_models_5_13_22.pdf")
ggplot(attr_deaths_all_yr_effect, aes(x = Time_Period, shape = model, color = model)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  geom_point(aes(y = attr_deaths)) +
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       # title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       # Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "",
       color = "",
       shape = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid"))+ 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE),
         shape=guide_legend(nrow=2,byrow=TRUE))+ 
  scale_shape_manual(values = c(19, 4, 17, 5))
dev.off()
## quartz_off_screen 
##                 2

7.4 Only Log Model All Years

cov_mat_main_analysis <- sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time_w_cov[[2]]
cov_mat_two_yr <- sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_w_cov[[2]]

plot_effects_only_log_all_yr_data <- data.frame(time_after_tx = rep(0:39, 2),
                                                coef = c(
                                                  sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                                    sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term ==
                                                      "Intervention_Redefined"] + 
                                                    sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$estimate[
                                                      sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term ==
                                                        "lag_num_pd_w_tx"]*c(0:39),
                                                  sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                                    sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                                                    sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                                                      sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term ==
                                                        "lag_num_pd_w_tx"]*c(0:39)))

plot_effects_only_log_all_yr_data$conf.low <- c(plot_effects_only_log_all_yr_data$coef[1:40] - 
  1.96*sqrt(cov_mat_main_analysis["Intervention_Redefined", "Intervention_Redefined"] + 
      2*c(0:39)*cov_mat_main_analysis["Intervention_Redefined", "lag_num_pd_w_tx"] + 
        c(0:39)^2*cov_mat_main_analysis["lag_num_pd_w_tx", "lag_num_pd_w_tx"]),
  plot_effects_only_log_all_yr_data$coef[41:80] - 
  1.96*sqrt(cov_mat_two_yr["int_2_yr_effect", "int_2_yr_effect"] + 
      2*c(0:39)*cov_mat_two_yr["int_2_yr_effect", "lag_num_pd_w_tx"] + 
        c(0:39)^2*cov_mat_two_yr["lag_num_pd_w_tx", "lag_num_pd_w_tx"]))

plot_effects_only_log_all_yr_data$conf.high <- c(plot_effects_only_log_all_yr_data$coef[1:40] + 
  1.96*sqrt(cov_mat_main_analysis["Intervention_Redefined", "Intervention_Redefined"] + 
      2*c(0:39)*cov_mat_main_analysis["Intervention_Redefined", "lag_num_pd_w_tx"] + 
        c(0:39)^2*cov_mat_main_analysis["lag_num_pd_w_tx", "lag_num_pd_w_tx"]),
  plot_effects_only_log_all_yr_data$coef[41:80] +
  1.96*sqrt(cov_mat_two_yr["int_2_yr_effect", "int_2_yr_effect"] + 
      2*c(0:39)*cov_mat_two_yr["int_2_yr_effect", "lag_num_pd_w_tx"] + 
        c(0:39)^2*cov_mat_two_yr["lag_num_pd_w_tx", "lag_num_pd_w_tx"]))

plot_effects_only_log_all_yr_data$model  <- c(rep("Lasting Effect", 40), rep("Two-Year Effect", 40))



pdf("./Figures/only_log_models_all_yr_effects_5_17_22_RR.pdf")
ggplot(plot_effects_only_log_all_yr_data, mapping = aes(x = time_after_tx)) + 
  geom_line(mapping = aes(y = exp(coef), linetype = "Estimated Effect", color = model)) +
  geom_point(mapping = aes(y = exp(coef), linetype = "Estimated Effect", shape = model, color = model)) +
  geom_line(plot_effects_only_log_all_yr_data[plot_effects_only_log_all_yr_data$model == "Lasting Effect",],
            mapping = aes(y = exp(conf.low), linetype = "95% CI for Model with Lasting Effect", color = model)) + 
  geom_line(plot_effects_only_log_all_yr_data[plot_effects_only_log_all_yr_data$model == "Lasting Effect",],
            mapping = aes(y = exp(conf.high), linetype = "95% CI for Model with Lasting Effect", color = model)) + 
  geom_line(plot_effects_only_log_all_yr_data[plot_effects_only_log_all_yr_data$model == "Two-Year Effect",],
            mapping = aes(y = exp(conf.low), linetype = "95% CI for Model with Two-Year Effect", color = model)) + 
  geom_line(plot_effects_only_log_all_yr_data[plot_effects_only_log_all_yr_data$model == "Two-Year Effect",],
            mapping = aes(y = exp(conf.high), linetype = "95% CI for Model with Two-Year Effect", color = model)) + 
  scale_linetype_manual(values = c("dashed", "dotdash", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Risk Ratio",
       color = "", shape = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 1), color = "grey", linetype = "dotted")  + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE),
         shape = guide_legend(nrow=2,byrow=TRUE)) + 
  scale_shape_manual(values = c(19, 4, 17, 5))
dev.off() 
## quartz_off_screen 
##                 2
colnames(attr_deaths_est_log_smoothed_time_lin_post_summary) <- 
  c("year", "attr_deaths", "attr_deaths_lb", "attr_deaths_ub")

attr_deaths_all_yr_effect_only_log <- rbind(cbind(attr_deaths_est_log_smoothed_time_lin_post_summary, 
                                                  "model" = "Lasting Effect"),
                                            cbind(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary, "model" = "Two-Year Effect"))


pdf("./Figures/lives_saved_only_log_models_5_17_22.pdf")
ggplot(attr_deaths_all_yr_effect_only_log, mapping = aes(x = year, shape = model, color = model)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Lasting Effect",],
            mapping = aes(y = attr_deaths, linetype = "Estimated Effect")) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Two-Year Effect",],
            mapping = aes(y = attr_deaths, linetype = "Estimated Effect")) + 
  geom_point(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Lasting Effect",],
             mapping = aes(y = attr_deaths)) +
  geom_point(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Two-Year Effect",],
             mapping = aes(y = attr_deaths)) +
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Lasting Effect",],
            mapping = aes(y = attr_deaths_lb, linetype = "95% CI for Model with Lasting Effect")) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Lasting Effect",],
            mapping = aes(y = attr_deaths_ub, linetype = "95% CI for Model with Lasting Effect")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Two-Year Effect",],
            mapping = aes(y = attr_deaths_ub, linetype = "95% CI for Model with Two-Year Effect")) + 
  geom_line(attr_deaths_all_yr_effect_only_log[attr_deaths_all_yr_effect_only_log$model == "Two-Year Effect",],
            mapping = aes(y = attr_deaths_lb, linetype = "95% CI for Model with Two-Year Effect")) +
  labs(x = "Date", y = "Yearly Number of Lives Saved",
       # title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       # Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "",
       color = "",
       shape = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom",
        legend.text = element_text(size = 8)) + 
  scale_linetype_manual(values = c("dashed", "dotdash", "solid"))+ 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE),
         shape=guide_legend(nrow=2,byrow=TRUE))+ 
  scale_shape_manual(values = c(19, 4, 17, 5))
dev.off()
## quartz_off_screen 
##                 2
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths)
## [1] 197297.8
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths_lb)
## [1] -73711.92
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths_ub)
## [1] 468307.4
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths)/sum(main_analysis_data$imputed_deaths)
## [1] 0.2997463
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths_lb)/sum(main_analysis_data$imputed_deaths)
## [1] -0.1119875
sum(attr_deaths_est_log_smoothed_time_lin_post_summary$attr_deaths_ub)/sum(main_analysis_data$imputed_deaths)
## [1] 0.71148
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths)
## [1] 67363.52
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)
## [1] -55713.72
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)
## [1] 190440.8
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths)/sum(main_analysis_data$imputed_deaths)
## [1] 0.1023426
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_lb)/sum(main_analysis_data$imputed_deaths)
## [1] -0.08464353
sum(attr_deaths_est_log_lin_time_lin_post_tx_2yr_summary$attr_deaths_ub)/sum(main_analysis_data$imputed_deaths)
## [1] 0.2893287